added "not"

master
Jordan Orelli 12 years ago
parent 175e8abf1b
commit 6395089039

@ -52,3 +52,4 @@ x
(define a1 (make-account 100.00)) (define a1 (make-account 100.00))
(a1 -20.00) (a1 -20.00)

@ -83,3 +83,10 @@ func division(vals []interface{}) (interface{}, error) {
} }
return a.total(vals) return a.total(vals)
} }
func not(vals []interface{}) (interface{}, error) {
if err := checkArity(1, vals, "not"); err != nil {
return nil, err
}
return !booleanize(vals[0]), nil
}

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

@ -71,6 +71,13 @@ func quote(_ *environment, args ...interface{}) (interface{}, error) {
return args[0], nil return args[0], nil
} }
func booleanize(v interface{}) bool {
if b, ok := v.(bool); ok {
return b
}
return true
}
// defines the built-in "if" contruct. e.g.: // defines the built-in "if" contruct. e.g.:
// //
// (if #t "foo" "bar") // (if #t "foo" "bar")
@ -90,10 +97,10 @@ func _if(env *environment, args ...interface{}) (interface{}, error) {
return nil, err return nil, err
} }
if b, ok := v.(bool); ok && !b { if booleanize(v) {
return eval(args[2], env) return eval(args[1], env)
} }
return eval(args[1], env) return eval(args[2], env)
} }
// defines the built-in "set" construct, which is used to set the value of an // defines the built-in "set" construct, which is used to set the value of an

Loading…
Cancel
Save