开发者

Basic Scheme Recursion

开发者 https://www.devze.com 2023-02-13 10:40 出处:网络
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

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开发者_StackOverflow中文版 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))


                                   ))))


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

精彩评论

暂无评论...
验证码 换一张
取 消