Error: Unable to bind name in null syntax environment

I am currently doing exercise 1.3 of the sikpa book. Here's a description of the problem:

Define a procedure that takes three numbers as arguments and returns the sum of the squares of two large numbers.

I tried to solve it with the following code

(define (square x) (* x x))

(define (sq2largest a b c)
        ((define large1 (if (> a b) a b)) 
         (define small  (if (= large1 a) b a))
         (define large2 (if (> c small) c small))
         (+ (square large1) (square large2))))

      

When I ran it in mit schema I got the following error:

; Unable to bind name in null syntax: large1 # [reserved element-name 13]

Failure to run this error doesn't give many results. Does anyone know what happened to my code? (I am not familiar with the Scheme)

+3


source to share


2 answers


You have too many parentheses. If you've extracted the extra parentheses around the inner definitions, things should work much better.



+3


source


I'll try to break the structure of your sqllargest procedure:

Basic structure:

(define (sq2largest a b c)
    ; Body)

      

The body you wrote is:

((define large1 (if (> a b) a b)) ; let this be alpha
 (define small  (if (= large1 a) b a)) ; let this be bravo
 (define large2 (if (> c small) c small)) ; let this be charlie
 (+ (square large1) (square large2)) ; let this be delta) ; This parentheses encloses body

      

So, the Body is structured as:



(alpha bravo charlie delta)

      

This means: "Pass bravo, charlie and delta as arguments to alpha".

Now alpha says to take a bunch of arguments, but inside the namespace reserved for large1, there was no condition for any argument ... i.e. the schema meets an empty syntax environment where it cannot bind any variable.

Parentheses are important in a schema (and most, if not all, Lisps) because they define the scope of a procedure and provide [1] the order in which operations are applied.

[1] "No ambiguity can arise because the operator is always the leftmost element and the whole combination is separated by parentheses." http://mitpress.mit.edu/sicp/full-text/sicp/book/node6.html

+3


source







All Articles