added "length"

master
Jordan Orelli 12 years ago
parent 6395089039
commit 8476cc531b

@ -53,3 +53,7 @@ x
(define a1 (make-account 100.00)) (define a1 (make-account 100.00))
(a1 -20.00) (a1 -20.00)
(not "dave")
(if (not #f) (quote "true-condition") (quote "false-condition"))
(length (quote (1 2 3)))

@ -2,6 +2,8 @@ package main
import ( import (
"errors" "errors"
"fmt"
"reflect"
) )
type builtin func([]interface{}) (interface{}, error) type builtin func([]interface{}) (interface{}, error)
@ -90,3 +92,21 @@ func not(vals []interface{}) (interface{}, error) {
} }
return !booleanize(vals[0]), nil return !booleanize(vals[0]), nil
} }
func length(vals []interface{}) (interface{}, error) {
if err := checkArity(1, vals, "length"); err != nil {
return nil, err
}
x, ok := vals[0].(sexp)
if !ok {
return nil, fmt.Errorf("first argument must be sexp, received %v", reflect.TypeOf(vals[0]))
}
return len(x), nil
}
/*
func car(vals []interface{}) (interface{}, error) {
}
*/

@ -27,6 +27,7 @@ var universe = &environment{map[symbol]interface{}{
"-": builtin(subtraction), "-": builtin(subtraction),
"*": builtin(multiplication), "*": builtin(multiplication),
"/": builtin(division), "/": builtin(division),
"length": builtin(length),
"not": builtin(not), "not": builtin(not),
"define": special(define), "define": special(define),
"quote": special(quote), "quote": special(quote),

Loading…
Cancel
Save