Python syntax in Lisp and Scheme
Pascal Bourguignon
spam at thalassa.informatimago.com
Wed Oct 15 23:16:17 EDT 2003
Jacek Generowicz <jacek.generowicz at cern.ch> writes:
> mertz at gnosis.cx (David Mertz) writes:
>
> > HOFs are not a special Lisp thing. Haskell does them much better,
> > for example... and so does Python.
>
> mertz at gnosis.cx (David Mertz) writes:
>
> > the alpha and omega of HOFs is that functions are first class
> > objects that can be passed and returned.
>
> How do you reconcile these two statements ?
>
> [Hint: functions in Lisps are "first class objects that can be passed
> and returned"; how does Python (or Haskell) do this "alpha and omega
> of HOFs" "much better" ?]
Well, to be, a first class object is an object I can "process". For
example, a list is a first class object:
[37]> (setq x '(a b c))
(A B C)
[42]> (setf (cadr x) 'd)
D
[43]> x
(A D C)
So, to be really a first class object, a function should let itself be
modified too:
[44]> (setq f (function (lambda (x) (+ 1 x))))
#<CLOSURE :LAMBDA (X) (+ 1 X)>
Oops, an opaque type, I can't change the body.
[46]> (setq f (lambda (x) (+ 1 x)))
#<CLOSURE :LAMBDA (X) (+ 1 X)>
No luck.
[47]> (setq f '(lambda (x) (+ 1 x)))
(LAMBDA (X) (+ 1 X))
Ok, ok, this is only a list...
[48]> (setf (second (caddr f)) 2)
2
[49]> f
(LAMBDA (X) (+ 2 X))
[50]> (funcall f 3)
*** - FUNCALL: argument (LAMBDA (X) (+ 2 X)) is not a function.
See, not a first class object...
[52]> (eval `(,f 3))
5
Ha! My precious, precious eval...
--
__Pascal_Bourguignon__
http://www.informatimago.com/
Do not adjust your mind, there is a fault in reality.
More information about the Python-list
mailing list