merits of Lisp vs Python

Alex Mizrahi udodenko at users.sourceforge.net
Fri Dec 8 15:02:59 EST 2006


(message (Hello 'Bjoern)
(you :wrote  :on '(Fri, 08 Dec 2006 19:57:28 +0100))
(

 ??>> also, there's no need for operator precendence to be taken in
 ??>> accound -- order is explicitly defined. expressions are
 ??>> homogenous, they do not depend on other expressions near them
 ??>> (except lexical variables and side effects).

 BS> Sorry, I don't get it ;) Where does Python have things like
 BS> nonexplicit defined operator order?

you have an expression 3 + 4 which yields 7.
you have an expression 4 * 1 which yields 4.
if you paste 3 + 4 in place of 1, you'll have 4 * 3 + 4 = 16. as we know, * 
is commutative, but 3 + 4 * 4 = 19.
so result depends on implicit operator precendence rules.

in lisp, if you paste (+ 3 4) in (* 4 1), you'll have (* 4 (+ 3 4)), and it 
does not depend on order of operands, (* (+ 3 4) 4) yields same results. you 
do not have to remember precendence rules -- * and + are not anyhow special 
from other functions. all you need to remember is how to call functions.

certainly it's a very simple example, but it shows that generally, homogeous 
lisp syntax make expressions much less dependant on context, and there are 
much less rules affecting it. thus, work require by brain to understand and 
write code can be reduced -- that way it's easier.

Common Lisp specification define internal languages that are not in 
homogenous s-expr syntax -- that is formatter and loop macro.
like

 (loop for i from 1 to 20 collect i)

or

(loop for e in list
    maximizing e into max
    minimizing e into min
  finally (return (values min max)))

remarkable thing about that is that it's the only place which i cannot 
remember and need to lookup quite frequently in the reference.
although it looks almost like plain english and looks quite easy, it's much 
harder than other parts of lisp that use uniform syntax.

but certainly this uniform syntax doesn't worth much without macro 
facilities.

i'll show an example from my recent experiments.
in this example we want to perform a query into a RDF database 
(triplestore), SPARQL-style query, and then we format results as HTML.
here's how it looks in the SPARQL (i'm not fluent in it, so it can be 
buggy). basically, we want to query information (firstname, lastname, 
salary) or all users in specified departament.

PREFIX myapp:   <https://mydomain.net/myapp/1.1/>
SELECT ?fname, ?lname, ?salary
WHERE
{
    ?user myapp:name ?uname.
    ?uname myapp:first-name ?fname.
    ?uname myapp:last-name ?lname.
    ?user myapp:salary ?salary.
    ?user myapp:department ?dept.
}

we should feed this text to the query-builder.
then we should bind ?dept to our variable departament (i'm not sure how this 
is done in SPARQL, but there should be a way).
then we should iterate over results and output HTML. a python-like 
pseudocode:

query = BuildQuery(query_str)
query.bind("?dept", departament)
results = query.execute()
for each rs in results:
    print "<tr><td>" + htmlesc(rs.get("?fname")) + "</td><td>" + 
htmlesc(rs.get("?lname")) + "</td><td>" + rs.get("?salary") + "</td></tr>"

so how uniform syntax and macros can help here? we can make a macro 
rdf:do-query that will both programmatically create query, bind input 
variables and then bind results into local variables. also we can wrap HTML 
output in a macro too.
so here's a code:

(rdf:do-query
 ((?user :name ?uname)
  (?uname :first-name ?fname)
  (?uname :last-name ?lname)
  (?user :salary ?salary)
  (?user :department department))
  (html (:tr
         (:td (:princ ?fname))
         (:td (:princ ?lname))
         (:td (:princ ?salary)))))

as you can find, it's two times less code, it's less clumsy, and there's 
even less quotes and parentheses there -- who says that lisp has more 
parentheses? also, this code is less error-prone, because some part of 
correctless can be checked during compilation time by macros, because it 
operates with symbols on semantic level, but not with simple strings.
for example, if i make a type and write ?fnme instead of ?fname in the HTML 
output, compiler will report a warning about unbound variable, but if this 
variable will be in string-form, compiler will not be able to.
there are other benefits: RDF query language is transparently integrated 
into Lisp, there's no need to learn some other language (SPARQL) syntax 
additionally. and it strains my brain less when i don't have to switch 
between different languages.

is it possible to construct such helper functions (or whatever) to simplify 
code in Python?

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"People who lust for the Feel of keys on their fingertips (c) Inity") 





More information about the Python-list mailing list