schememit-scheme

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


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))))