Ben
Ben

Reputation: 2917

Basic Scheme Recursion

I have a file like this:

    declare
    a = aexpress 
    b = bexpress 
    begin 

My scheme program sets the current input port to this file then calls (declarations (read)) What im getting back, is #f. or rather the console says "The object #f is not applicable."

I have been over my parenthesis use and cant find any reason it should be returning a boolean value, but I'm sure I'm missing something.

What I want, is ((a aexpress) (b bexpress))

(define declarations
  (lambda (token)
    (cond (((eq? token 'begin) '())
           (else (let* ((name token)
                        (eqsign (read))
                        (value (read)))
                   (cons (list name value) (declarations (read)))))))))

Called by:

    (define convert
      (lambda (filename)

         (begin
           (set-current-input-port! (open-input-file filename))
           (statement (read))
           )

        )
      )
 (define statement (lambda (token) (

                                   cond (
                                     ( (eq? token 'declare) (declarations (read)) )
                                        ;       ( (eq? token 'declare) (declare_statement)  )
                                        ;       ( (eq? token 'begin) (begin_statement) )
                                        ;       ( (eq? token 'for) (for_statement) )
                                        ;       ( (eq? token 'if) (if_statement)  )
                                        ;       ( (eq? token 'set) (set_statement) )
                                        ;       (else (expression_token))


                                   ))))

Upvotes: 0

Views: 732

Answers (1)

C. K. Young
C. K. Young

Reputation: 222973

I've fixed the code formatting for you, which reveals what the problem is: you have too many layers of brackets around the (eq? token 'begin). The fixed version would look like this:

(define declarations
  (lambda (token)
    (cond ((eq? token 'begin) '())
          (else (let* ((name token)
                       (eqsign (read))
                       (value (read)))
                  (cons (list name value) (declarations (read))))))))

Upvotes: 5

Related Questions