syntax error - Scheme notation for "and" conditional -


for sicp course learning scheme , had assignment asked check if point in axis-aligned rectangular. did this:

(define in-rect?   (lambda (px py rx1 ry1 rx2 ry2)      (<= (* (- px rx1) (- px rx2)) 0) ,     (<= (* (- py ry1) (- py ry2)) 0)))  

i did according previous c habits , forgot polish notation while there. interpreter our online tutor program uses runs code "correctly", intended. however, afaik, usage of 'and' should syntactically wrong. drracket points out syntax error when try run this.

then how did evaluate correct values every test case on online tutor? option valid maybe?

the syntax and same expression - uses prefix notation; can have 0 or more arguments, don't have boolean expressions:

(and <exp1> <exp2> <exp3> ...) 

for code should follows:

(define in-rect?   (lambda (px py rx1 ry1 rx2 ry2)      (and (<= (* (- px rx1) (- px rx2)) 0)          (<= (* (- py ry1) (- py ry2)) 0)))) 

as why code seemed work in online tutor, it's because interpreter evaluated body of lambda inside implicit begin 3 expression (first <= expression, and special form, second <= expression), returning value of last condition. although it's weird worked @ all, because and without arguments rises "bad syntax" or similar error - depends on how implemented, doesn't seem standards-compliant interpreter.

to make sure everything's clear, take @ documentation, because and behaves different you'd expect, coming c background. remember and short-circuits @ first false condition:

(and (+ 1 1) #f (/ 1 0)) ; work without division 0 error              ^    evaluation stops here => #f 

and notice and returns value of last expression encounters, because in scheme not explicitly #f, considered true:

(and #t (* 2 3) (+ 1 1))                    ^          evaluation stops here => 2 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -