Why is Python popular, while Lisp and Scheme aren't?
Pascal Costanza
costanza at web.de
Sun Nov 10 17:09:05 EST 2002
David Garamond wrote:
> Jacek Generowicz wrote:
[...]
> yes, that's good and all. the only problem is, most people _don't
> like_it. they are _allergic_ to ((())). so what gives? the things that
> will make them like:
>
> (+ 1 1)
>
> over (or the same as):
>
> 1+1
>
> are probably only brain surgery and heavy brainwashing.
...or some nice examples. ;)
> (+ 1 1)
2
> (+ 1 1 1)
3
> (+ 1 1 1 2)
5
> (+ 2)
2
> (+)
0
> (*)
1
The last two examples are a bit strange - why would you want to apply
"+" to no arguments? Well, this can be very handy when you want to
generate code in one part of your program (for example, in macros), but
you don't want to care about special cases that may occur wrt number of
arguments.
Now, let's go on - "funcall" takes a function and applies it to the
remaining arguments.
> (funcall '+ 1 1 1)
3
> (funcall '* 2 3)
6
...and so on. Now here is another nice macro.
(defmacro apply-operators (op-list arg-list)
`(loop for op in ',op-list
do (format t " ~A: ~A~%" op (funcall op , at arg-list))))
* Remember that macros get code as input and generate code as output.
(See another recent post of mine.)
* ` means: create a list but evaluate everything that's marked with a ,
or ,@
* ',op-list means: evaluate op-list when processing this macro, but
quote the result (regard it as data, not code) in the code that is
generated. (See below.)
* ,@ evaluates what follows, unwraps one pair of parentheses and inserts
the result in the surrounding list
* (format t ...) produces formatted output on standard output. ~A means:
take one argument and print it. ~% means: start a new line.
So here is an application.
> (apply-operators (+ * min max) (5 4 6 3 7))
+: 25
*: 2520
min: 3
max: 7
You can always take a look at the code a specific macro produces.
> (macroexpand-1 '(apply-operators (+ * min max) (5 4 6 3 7)))
(loop for op in '(+ * min max)
do (format t " ~A: ~A~%" op (funcall op 5 4 6 3 7)))
Pascal
--
Given any rule, however ‘fundamental’ or ‘necessary’ for science, there
are always circumstances when it is advisable not only to ignore the
rule, but to adopt its opposite. - Paul Feyerabend
More information about the Python-list
mailing list