merits of Lisp vs Python

Paul Rubin http
Fri Dec 15 09:43:21 EST 2006


André Thieme <address.good.until.2006.dec.22 at justmail.de> writes:
> def nif(num, pos, zero, neg):
>    if num > 0:
>      return pos
>    else:
>      if num == 0:
>        return zero
>      else:
>        return neg

def nif(num, pos, zero, neg):
   return (neg, zero, pos)[cmp(num, 0)+1]

> The messages were printed in each case.
> To stop that I need lazy evaluation:
> CL-USER> (mapcar #'(lambda (x)
> 		     (funcall
> 		      (nif x
> 			   #'(lambda () (p))
> 			   #'(lambda () (z))
> 			   #'(lambda () (n)))))
> 		 '(0 2.5 -8))

in Python:

    def lazy_nif(num, pos, zero, neg):
       return (neg, zero, pos)[cmp(num, 0)+1]()   # the () at the end means funcall

    map(lambda x: lazy_nif(x, p, z, n), (0, 2.5, -8))

"nif" is even cleaner in Haskell, if I have this right:

    nif x p z n | (x < 0) = n
                | (x == 0) = z
                | (x > 0)  = p

All Haskell evaluation is automatically lazy, so no lambdas etc. needed.



More information about the Python-list mailing list