racketthe-little-schemer

little schemer drracket error cannot reference an identifier before its definition


beginner question, just started the little schemer book and installed DrRacket on my macbook to try some of the code examples.

If I choose Racket language, the following code

 #lang Racket

(define (atom? x)
  (and (not (pair? x)) (not (null? x))))

(atom? '()) 



(define lat?
    (lambda (l)
        (cond
            ((null? l) #t)
            ((atom? (car l)) (lat? (cdr l)) )
            (else #f))))
(lat? (a b))       

will trigger error message:

a: unbound identifier in module in: a

if I choose R5RS language,

#lang R5RS

(define (atom? x)
  (and (not (pair? x)) (not (null? x))))

(atom? '()) 



(define lat?
    (lambda (l)
        (cond
            ((null? l) #t)
            ((atom? (car l)) (lat? (cdr l)) )
            (else #f))))
(lat? (a b))  

I got an error message:

#%plain-module-begin: illegal use (not a module body) in: (#%plain-module-begin (module configure-runtime racket/base (require r5rs/init)) (define (atom? x) (and (not (pair? x)) (not (null? x)))) (atom? (quote ())) (define lat? (lambda (l) (cond ((null? l) #t) ((atom? (car l)) (lat? (cdr l))) (else #f)))) (lat? (a b))) 

Anyone know what I did wrong?

Thanks


Solution

  • Looks like that last call should be

    (lat? '(a b))
    

    ... no?

    (Also: I would recommend using #lang racket in general, but I strongly suspect that your problem R5RS arises because you're "setting the language twice"; if you start your program with #lang R5RS, you don't need to change the language level. Conversely, if you set the language level, you shouldn't start your program with #lang R5RS. If you do both, I'm guessing you get the error message you saw.)