added "not"

master
Jordan Orelli 12 years ago
parent 175e8abf1b
commit 6395089039

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

@ -83,3 +83,10 @@ func division(vals []interface{}) (interface{}, error) {
}
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(multiplication),
"/": builtin(division),
"not": builtin(not),
"define": special(define),
"quote": special(quote),
"if": special(_if),

@ -71,6 +71,13 @@ func quote(_ *environment, args ...interface{}) (interface{}, error) {
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.:
//
// (if #t "foo" "bar")
@ -90,10 +97,10 @@ func _if(env *environment, args ...interface{}) (interface{}, error) {
return nil, err
}
if b, ok := v.(bool); ok && !b {
return eval(args[2], env)
if booleanize(v) {
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

Loading…
Cancel
Save