diff --git a/index.js b/index.js index 3b0f378..c4fa29e 100644 --- a/index.js +++ b/index.js @@ -116,24 +116,36 @@ let methods = { return handle(args[0]) == handle(args[1]) }, 'and':function(args) { - for(let arg of args) { - if(!handle(arg)) { - return false - } + if(args.length == 0) { + return true } - return true + let car = handle(args[0]) + let cdr = args.slice(1) + if(car === false) { + return false + } + if(cdr.length == 0) { + return car + } + return handle(['and'].concat(cdr)) }, 'or':function(args) { - for(let arg of args) { - if(handle(arg)) { - return true - } + if(args.length == 0) { + return false + } + let car = handle(args[0]) + let cdr = args.slice(1) + if(car !== false) { + return car } - return false + return handle(['or'].concat(cdr)) }, 'not':function(args) { - let value = handle(args[0]) - return !value + let car = handle(args[0]) + if(car !== false) { + return false + } + return !car }, // ----------------------------------------------------- 'null?':function(args) { diff --git a/test.js b/test.js index eca90ee..1a309ec 100644 --- a/test.js +++ b/test.js @@ -100,5 +100,23 @@ test('specific scheme env', t => { t.is(sc2.interpret("a"), 5) t.is(sc3.interpret("(define a 7)"), 7) t.is(sc3.interpret("a"), 7) +}) + +test('and', t => { + t.is(scheme('(and #t #t #f)'),false) + t.is(scheme('(and #f #t #t)'),false) + t.is(scheme('(and #t #t #t)'),true) + t.is(scheme('(and #t #t 5)'),5) + t.is(scheme('(and 5 #t 7)'),7) + t.is(scheme('(and 5 #f 7)'),false) + t.deepEqual(scheme("(and 5 #t '(a b))"),['list','a','b']) +}) +test('or', t => { + t.is(scheme('(or #f #f #t)'),true) + t.is(scheme('(or #f #t #f)'),true) + t.is(scheme('(or #f 7 #f)'),7) + t.is(scheme('(or 5 7 #f)'),5) + t.is(scheme('(or #f #f #f)'),false) + t.deepEqual(scheme("(or '(a b) #f #f)"),['list','a','b']) }) \ No newline at end of file