From: Hoan Ton-That
Subject: Partial application syntax (how to read a write macro)
Date: 
Message-ID: <1146815266.479888.316380@j73g2000cwa.googlegroups.com>
Hello Looney Lispers,

Although Lisp has always added had extensible syntax, it is rarely
extended.  The reason for this is that having too much syntax makes it
harder to reason about the S-expression underneath.  Good read macros
should make programs smaller, but not obscure the S-expression.

One area that could be made shorter with syntax is partial application
of functions.  [+ 10] is a partial application of + to 10.  It is
basically the same as (lambda (x) (+ 10 x)).  So we could write
(mapcar [+ 10] '(1 2 3 4 5)) instead of the longer version.  Lets do
it!

(defun enable-pf-reader ()
  (set-macro-character  #\[ #'pf-reader t)
  (set-syntax-from-char #\] #\))

`enable-pf-reader' calls the `pf-reader' function when we hit a `]'.
When we hit a `]', we do whatever we do when we hit a `)'.  The effect
is to return a list of all the symbols in between.

(defun pf-reader (stream char)
  (let ((list (read-delimited-list #\] stream t)))
    `(lambda (&rest args)
       (apply #',(first list) ,@(rest list) args)))

`pf-reader' reads until we hit the `]'.  Then it constructs the
appropriate lambda list.  Just by looking at the definition you can
tell that anything between `[' and `]' will return a function.

Note that its only a total of seven lines.  The hard part is not the
writing
of the read macros, but which ones to write.

Lets see how useful this is:

CL-USER> (enable-pf-syntax)
T
CL-USER> (mapcar [+ 10] '(1 2 3 4 5))
(11 12 13 14 15)
CL-USER> (mapcar [nthcdr 2] '((1 2 3 4) (1 2 3) (1 2) (1)))
((3 4) (3) NIL NIL)
CL-USER> (remove-if [= 1] '(1 2 3 2 1))
(2 3 2)

Notice that the syntax works on functions with optional, keyword and
rest arguments.  Another useful feature would be able to use `_' as a
place holder.  So for example:

CL-USER> (mapcar [reduce #'/ _ :from-end t] '((1 2 3 4) (4 3 2 1)))
(3/8 8/3)
CL-USER> (mapcar [reduce #'/]               '((1 2 3 4) (4 3 2 1)))
(1/24 2/3)

This is left as an exercise for reader.  Pun intended.  Alas, there
are very few delimiters to go around, so choose your read-macros
wisely.  Try to think of what you can do with <>{}!  Also if you have
any cool read macros, I'd like to see them.

Hoan

From: sross
Subject: Re: Partial application syntax (how to read a write macro)
Date: 
Message-ID: <1146829657.616735.95920@j73g2000cwa.googlegroups.com>
Another good one is the #L dispatch macro in the iterate
package.
it allows you to write anonymous functions like this

(mapcar #L(+ !1 10) '(1 2 3 4 5)) 

Cheers,
  Sean.
From: Pascal Bourguignon
Subject: Re: Partial application syntax (how to read a write macro)
Date: 
Message-ID: <87ejz8zh4d.fsf@thalassa.informatimago.com>
"Hoan Ton-That" <············@gmail.com> writes:
> This is left as an exercise for reader.  Pun intended.  Alas, there
> are very few delimiters to go around, so choose your read-macros
> wisely.  Try to think of what you can do with <>{}!  Also if you have
> any cool read macros, I'd like to see them.

If you allow Unicode, you've got at least 16 pairs of nice
parentheses, and up to 50 if you use the BLOCK drawings, and that only
counting the characters for which I've got a font for.

«» {}()[]「」 ‹› 〈〉【】「」{} 《》 “” ‘’ [] 〔〕『』〖〗

╔ ╗ ╚ ╝ ╠ ╣ ┎ ┒ ┟ ┧ ┍ ┑ ┡ ┩ ┏ ┓ ┗ ┛ ┣ ┫ ┌ ┐ └ ┘ ├ ┤ ┞ ┦ ┖ ┚ ┢ ┪ ┕ ┙ ┠

┨ ┝ ┥ ╅ ╅ ╆ ╆ ┭ ┭ ┮ ┮ ┵ ┵ ┶ ┶ ┽ ┽ ┾ ┾ ┱ ┱ ┲ ┲ ┹ ┹ ┺ ┺ ╉ ╉ ╊ ╊ ╃ ╃ ╄ ╄


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Do not adjust your mind, there is a fault in reality"
 -- on a wall many years ago in Oxford.
From: Burton Samograd
Subject: Re: Partial application syntax (how to read a write macro)
Date: 
Message-ID: <873bfme5rn.fsf@gmail.com>
"Hoan Ton-That" <············@gmail.com> writes:
<snip howto define ineresting reader macro>
> CL-USER> (mapcar [+ 10] '(1 2 3 4 5))
> (11 12 13 14 15)
> CL-USER> (mapcar [nthcdr 2] '((1 2 3 4) (1 2 3) (1 2) (1)))
> ((3 4) (3) NIL NIL)
> CL-USER> (remove-if [= 1] '(1 2 3 2 1))
> (2 3 2)

That's f'ing cool.  Thanks for that.

-- 
burton samograd					kruhft .at. gmail
kruhft.blogspot.com	www.myspace.com/kruhft	metashell.blogspot.com