Contents

How to use Python like Lisp

Contents

Lisp has some very effective way to get jobs done, this article give you a direct way to use Python like Lisp.

1
2
3
4
5
6
7
8
cons = lambda el, lst: (el, lst) 
mklist = lambda *args: reduce(lambda lst, el: cons(el, lst), reversed(args), None) 
car = lambda lst: lst[0] if lst else lst 
cdr = lambda lst: lst[1] if lst else lst 
nth = lambda n, lst: nth(n-1, cdr(lst)) if n > 0 else car(lst) 
length = lambda lst, count=0: length(cdr(lst), count+1) if lst else count
begin = lambda *args: args[-1] 
display = lambda lst: begin(w("%s " % car(lst)), display(cdr(lst))) if lst else w("nil\n")

where w = sys.stdout.write

1
2
3
foldr = lambda f, i: lambda s: reduce(f, s, i)
foldl = reduce
mapcar = map

Pay attention about the speed when you use Python, here is a benchmarks for 5 languages:

 from The Great Computer Language Shootout .

Test

Lisp

Java

Python

Perl

C++

hash access

1.06

3.23

4.01

1.85

1.00

exception handling

0.01

0.90

1.54

1.73

1.00

Legend

sum numbers from file

7.54

2.63

8.34

2.49

1.00

> 100 x C++

reverse lines

1.61

1.22

1.38

1.25

1.00

50-100 x C++

matrix multiplication

3.30

8.90

278.00

226.00

1.00

10-50 x C++

heapsort

1.67

7.00

84.42

75.67

1.00

5-10 x C++

array access

1.75

6.83

141.08

127.25

1.00

2-5 x C++

list processing

0.93

20.47

20.33

11.27

1.00

1-2 x C++

object instantiation

1.32

2.39

49.11

89.21

1.00

< 1 x C++

word count

0.73

4.61

2.57

1.64

1.00

Median

1.67

4.61

20.33

11.27

1.00

25% to 75%

0.93 to 1.67

2.63 to 7.00

2.57 to 84.42

1.73 to 89.21

1.00 to 1.00

Range

0.01 to 7.54

0.90 to 20.47

1.38 to 278

1.25 to 226

1.00 to 1.00

Relative Resource: Python for Lisp Programmers     by Peter Norvig