added cons, car, cdr

master
Jordan Orelli 12 years ago
parent 86929974cc
commit c648ad597d

@ -142,6 +142,42 @@ func issymbol(vals []interface{}) (interface{}, error) {
return ok, nil
}
func cons(vals []interface{}) (interface{}, error) {
if err := checkArity(2, vals, "cons"); err != nil {
return nil, err
}
s := sexp{vals[0]}
switch t := vals[1].(type) {
case sexp:
return append(s, t...), nil
default:
return append(s, t), nil
}
panic("not reached")
}
func car(vals []interface{}) (interface{}, error) {
if err := checkArity(1, vals, "car"); err != nil {
return nil, err
}
s, ok := vals[0].(sexp)
if !ok {
return nil, errors.New("expected list")
}
return s[0], nil
}
func cdr(vals []interface{}) (interface{}, error) {
if err := checkArity(1, vals, "cdr"); err != nil {
return nil, err
}
s, ok := vals[0].(sexp)
if !ok {
return nil, errors.New("expected list")
}
return s[1:], nil
}
type cmp_bin_i func(int64, int64) bool
type cmp_bin_f func(float64, float64) bool

@ -35,12 +35,19 @@ var universe = &environment{map[symbol]interface{}{
">=": builtin(gte),
"<": builtin(lt),
"<=": builtin(lte),
"cons": builtin(cons),
"car": builtin(car),
"cdr": builtin(cdr),
"length": builtin(length),
"list": builtin(list),
"list?": builtin(islist),
"not": builtin(not),
"null?": builtin(isnull),
"symbol?": builtin(issymbol),
// "="
// "equal?"
// "eq?"
// "append"
// special forms
"begin": special(begin),

Loading…
Cancel
Save