Python syntax in Lisp and Scheme

Bengt Richter bokr at oz.net
Sat Oct 4 05:16:02 EDT 2003


On 4 Oct 2003 00:17:30 -0700, grzegorz at pithekos.net (Grzegorz Chrupala) wrote:

>jcb at iteris.com (MetalOne) wrote in message news:<92c59a2c.0310031345.57d20631 at posting.google.com>...
>> Scheme
>> (define vector-fill!
>>   (lambda (v x)
>>     (let ((n (vector-length v)))
>>       (do ((i 0 (+ i 1)))
>>           ((= i n))
>>           (vector-set! v i x)))))
>> 
>> Python
>> def vector_fill(v, x):
>>     for i in range(len(v)):
>>         v[i] = x
>> 
I guess you could also just write

    v[:] = [x]*len(v)

instead of calling a function (though it takes more space), e.g.,

 >>> v=range(10)
 >>> id(v),v
 (9442064, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 >>> v[:] = [55]*len(v)
 >>> id(v),v
 (9442064, [55, 55, 55, 55, 55, 55, 55, 55, 55, 55])

using id(v) to show that the same object is mutated.

>> To me the Python code is easier to read, and I can't possibly fathom
>> how somebody could think the Scheme code is easier to read.  It truly
>> boggles my mind.
>
>Pick a construct your pet language has specialized support, write an
>ugly equivalent in a language that does not specifically support it
>and you have proved your pet language to be superior to the other
>language. (I myself have never used the "do" macro in Scheme and my
>impression is few people do. I prefer "for-each", named "let" or the
>CL-like "dotimes" for looping).
>The point is if you want you can easily implement something like
>"range" in Scheme (as shown by other posters). It would be more
>illustrative choose an area where one of the languages is inherently
>underpowered. For example I was shocked at how awkward Paul Graham's
>"accumulator generator" snippet is in Python:
>
>class foo:
>                        def __init__(self, n):
>                            self.n = n
>                        def __call__(self, i):
>                            self.n += i
>                            return self.n
>
Do you like this better?

 >>> def foo(n):
 ...     box = [n]
 ...     def foo(i): box[0]+=i; return box[0]
 ...     return foo
 ...
 >>> bar = foo(10)
 >>> bar(1)
 11
 >>> bar(2)
 13
 >>> bar(37)
 50
 >>> baz = foo(100)
 >>> bar(1), baz(23)
 (51, 123)

>
>> If a set of macros could be written to improve LISP syntax, then I
>> think that might be an amazing thing.  An interesting question to me
>> is why hasn't this already been done.
>
>There are libraries that let you write Scheme in Python-like
>indentation syntax (<URL:http://cliki.tunes.org/Scheme>, look for
>Alternative Syntaxes). However, they are not widely used.
>
>Cheers,
>--
>Grzegorz

Regards,
Bengt Richter




More information about the Python-list mailing list