Why is Python popular, while Lisp and Scheme aren't?

Pascal Costanza costanza at web.de
Mon Nov 11 18:39:03 EST 2002


Neil Schemenauer wrote:
> Jacek Generowicz wrote:
> 
>>I beg to differ.
>>
>>You *can* use Lisp as a Python-with-s-expressions. Once you get over
>>the parentheses (let's not argue whether one can get over them or
>>not), it really is very simple, if you stick to the simple things. 
> 
> Show me the Lisp code that corresponds to the Python code:
> 
>     float(somestring)
> 
> 'read' does't count since the string could have come from an evil
> outside source.  

 > (let ((*read-eval* nil))
     (with-input-from-string (s "3.1415927")
       (read s)))
3.1415927

Ugly because of the *read-eval* bit, but if you know that your string 
does not come from an evil source you're safe to leave it out. The nice 
thing here is that you don't need to know in advance what kind of datum 
you get.

 > How do I write
> 
>     def escape(s):
>         s = s.replace("&", "&")
>         s = s.replace("<", "<")
>         s = s.replace(">", ">")
>         s = s.replace('"', """)
>         return s
> 
> plainly and efficiently in Lisp?

(defun escape (s)
   (with-output-to-string (out)
     (with-input-from-string (in s)
       (loop while (listen in)
             do (let ((char (read-char in)))
                  (case char
                    (#\& (write-string "&" out))
                    (#\< (write-string "<" out))
                    (#\> (write-string ">" out))
                    (#\" (write-string """ out))
                    (otherwise (write-char char out))))))))

* with-output-to-string declares out as an output stream. It returns 
whatever has been written to out as a string.

* listen determines whether there are still data left to be read on the 
given stream.

* This solution is inherently more efficient because it traverses the 
input exactly once, whereas your solution traverses the input four times.

>     import socket
>     s = socket.socket()
>     s.connect(("localhost", 13))
>     print s.recv(1024)
>     s.close()

Common Lisp doesn't have a standardized socket library and I don't know 
enough about those that are available.


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