merits of Lisp vs Python

Neil Cerutti horpner at yahoo.com
Tue Dec 12 23:13:30 EST 2006


On 2006-12-13, hit_the_lights <langstefan at gmx.at> wrote:
> Neil Cerutti schrieb:
>
>> >>   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.

That's cool. Thanks for posting the code.

Is the above 'duck-typing' idiom considered very useful to a
Lisper? It seems logical to me that duck-typing works best in an
environment where it is ubiquitous. If users have to implement
accessors specifically to use your library, it is not as good as
if they had already implemented one as a matter of routine.

-- 
Neil Cerutti



More information about the Python-list mailing list