Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SICP 1.3 interpreter error unknown identifier: and

I am using the interpreter from browser (without any local setup): https://inst.eecs.berkeley.edu/~cs61a/fa14/assets/interpreter/scheme.html
and getting the following interpreter exception message:

SchemeError: unknown identifier: and

Current Eval Stack:
-------------------------
0: and
1: (cond (and (< x y) (< x z)) (sqrt-sum y z))
2: (f 1 2 3)

for the following code:

; define a procedure that takes three numbers
; as arguments and returns the sum of the squares
; of the two larger numbers

(define (square) (* x x))
(define (sqrt-sum x y) 
  (+ (square x) (square y)))

(define (f x y z)
      (cond (and (< x y) (< x z)) (sqrt-sum y z))
      (cond (and (< y x) (< y z)) (sqrt-sum x z))
      (cond (and (< z y) (< z x)) (sqrt-sum x y)))

(f 1 2 3)

I am struggling to find any info about specific Scheme version this interpreter is based on; sorry

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s not the correct syntax for cond. The syntax is

(cond (condition1 value1...)
      (condition2 value2...)
      ...)

In your code the first condition should be the expression (and (< x y) (< x z)). But you don’t have the parentheses around the condition and value. You have just and where the condition should be, not (and (< x y) (< x z)). Since and isn’t a variable with a value, you get an error because of that.

The correct syntax is:

(define (f x y z)
  (cond ((and (< x y) (< x z)) (sqrt-sum y z))
        ((and (< y x) (< y z)) (sqrt-sum x z))
        ((and (< z y) (< z x)) (sqrt-sum x y))))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading