merits of Lisp vs Python

hit_the_lights langstefan at gmx.at
Tue Dec 12 20:25:14 EST 2006


Neil Cerutti schrieb:

> On 2006-12-12, André Thieme <address.good.until.2006.dec.22 at justmail.de> wrote:
> >> Contrast the much more common
> >>
> >>   a[i] = b[n]
> >>
> >> with
> >>
> >>   (setf (aref a i) (aref b n))
> >>
> >> and the attractions of Python may make more sense.
> >
> > Here Python and Lisp are equal, 7 tokens vs 7 tokens, but in
> > Python one has to write less since "[]" are 2 chars while
> > "aref" are 4, plus the setf.  But from counting the brain units
> > which I regard as an important factor they are both equal.
>
> A comparison of brain units of the above snippets is irrelevant,
> since the snippets are not equivalent.
>
> The Python snippet will work for any object a that provides
> __setitem__ and any object b that provides __getitem__.
>
> I don't know what an equivalent Lisp snippet would be (or even
> exactly how close the above snippet comes to matching the Python
> code), but whatever it is would be a better foundation for
> comparing brain units with the above Python.

It would be exactly like the example given, just "aref" replaced with
something else. An example implementation:

==========================================
(defgeneric $ (container key))
(defgeneric (setf $) (value container key))

;;; Implementation for arrays

(defmethod $ ((container array) (key integer))
  (aref container key))

(defmethod (setf $) (value (container array) (key integer))
  (setf (aref container key) value))
==========================================

And usage:

==========================================
CL-USER(3): (defparameter a (vector 1 2 3 4 5))
A
CL-USER(4): ($ a 0)
1
CL-USER(5): (setf ($ a 0) 9)
9
CL-USER(6): a
#(9 2 3 4 5)
==========================================

The nice thing is, that you *can* dispatch on the container,
the key and the value.




More information about the Python-list mailing list