1 """Standard Lisp primitives
3 Lisp type Python representation
4 --------- ---------------------
14 __all__
= ["cons", "car", "cdr", "null_p", "pair_p", "pylist",
15 "cadr", "caar", "cddr", "caddr", "caadr", "cdadr", "cadddr",
16 "set_car", "set_cdr", "symbol", "list", "map",
17 "true", "false", "eq_p", "number_p", "symbol_p", "string_p",
18 "display", "newline", "symbol_to_string",
25 __slots__
= ('car', 'cdr')
26 def __init__(self
, car
, cdr
):
30 return pair_p(o
) and self
.car
== o
.car
and self
.cdr
== o
.cdr
33 return type(p
) is cons
39 if pair_p(p
): return p
.car
40 else: error("CAR: expects a non-empty <pair>; given %s", p
)
43 if pair_p(p
): return p
.cdr
44 else: error("CDR: expects a non-empty <pair>; given %s", p
)
47 if pair_p(p
): p
.car
= v
48 else: error("CAR: expects a non-empty <pair>; given %s", p
)
51 if pair_p(p
): p
.cdr
= v
52 else: error("CDR: expects a non-empty <pair>; given %s", p
)
54 # TODO: complete the combinations
55 def cadr(x
): return car(cdr(x
))
56 def caar(x
): return car(car(x
))
57 def cddr(x
): return cdr(cdr(x
))
58 def caddr(x
): return car(cdr(cdr(x
)))
59 def caadr(x
): return car(car(cdr(x
)))
60 def cadar(x
): return car(cdr(car(x
)))
61 def cdadr(x
): return cdr(car(cdr(x
)))
62 def caddr(x
): return car(cdr(cdr(x
)))
63 def cadddr(x
): return car(cdr(cdr(cdr(x
))))
67 for a
in reversed(args
):
71 def map(function
, ls
):
74 result
.append(function(car(ls
)))
81 result
.append(car(ls
))
90 def __init__(self
, string
):
93 return hash(self
.string
)
95 return type(o
) is symbol
and self
.string
== o
.string
97 return "<symbol: %s>" % self
.string
101 return type(sym
) is symbol
103 def symbol_to_string(sym
):
104 if symbol_p(sym
): return sym
.string
105 else: error("symbol->string: expets a <symbol>; given %s", sym
)
107 def string_to_symbol(s
):
120 return type(n
) in (int, float)
123 return type(s
) is str
136 globals()["+"] = lambda *args
: sum(args
)
137 globals()["-"] = lambda a
,b
: a
-b