[racket] Code Error Creating List

From: Todd Dobmeyer (dobmeyer.2 at wright.edu)
Date: Tue May 17 20:03:29 EDT 2011

I am taking a class at Wright State University and we are working on 
problem 3.27 from the EOPL 2nd Edition. This problem has us modifying 
the closure procedure to only bind the free variables in our environment 
instead of storing all the variables. I am trying to write some code 
that will create a list of all the variables in my expression. This will 
then be passed to a difference procedure (that has been provided) to 
determine which variables are free. Unfortunately, every time I run my 
procedure, I receive the error: "procedure application: expected 
procedure, given: x (no arguments)" What I should be returning is the 
list '(x) as "x" is the only variable in my simple expression. The 
string I am running is as follows:

(define str2 "let x = 15 y = 27
               in let f = proc () x
                  in let x = 8
                     in (f)")

I then run the command:

(run str2)

After it works through its beginning work, it finally hits the proc-exp 
where it calls closure. My closure function calls me free-vars procedure 
but it apparently does not return a list like I think it should. I have 
tried numerous ways and always get this same error. Any ideas on where I 
am going wrong would be greatly appreciated. I'm sure there are errors 
inside my logic, but if anybody can help me figure out why I am not 
returning a list out of (free-vars) that would be great! My code looks like:

(define free-vars
   (lambda (body)
     (list (free-vars-list body))
   )
)

(define free-vars-list
   (lambda (body)
     (
       (if (list? body) (append (free-vars-list (car body)) 
(free-vars-list (cdr body)))
                        (cases expression body
                           ;; When we have a var-exp, return the variable
                           (var-exp (id) id)
                           (primapp-exp (prim rands) 
(append(free-vars-list (car rands)) (free-vars-list (cdr rands))))
                           (if-exp (test-exp true-exp false-exp)
                                   (append (free-vars-list test-exp) 
(free-vars-list true-exp) (free-vars-list false-exp)))
                           (begin-exp (exp1 exps) (append 
(free-vars-list exp1) (free-vars-list exps)))
                           (let-exp (ids rands body) (append 
(free-vars-list body)))
                           (proc-exp (ids body) (append (free-vars-list 
body)))
                           (app-exp (rator rands) (append 
(free-vars-list rator) (free-vars-list rands)))
                           (else '())
                        )
       )
     )
   )
)

(define closure
   (lambda (ids body env)
     (let ((todd (free-vars body)))
     (let ((freevars (difference todd ids)))
       (let ((saved-env
                    (extend-env
                          freevars
                          (map (lambda (v) (apply-env env v)) freevars)
                          (empty-env))
            ))
          (lambda (args)
                   (eval-expression body (extend-env ids args env)))
       )
     )
   ))
)

My call stack looks like:

procedure application: expected procedure, given: x (no arguments)

C:\Users\Todd\Documents\CS784\Assign2\3-5-mod.scm: 255:4
         (if (list? body) (append (free-vars-list (car body)) 
(free-vars-list (cdr body)))
                        (cases expression body
                           ;; When we have a var-exp, return the variable
                           (var-exp (id) id)
                           (primapp-exp (prim rands) 
(append(free-vars-list (car rands)) (free-vars-list (cdr rands))))
                           (if-exp (test-exp true-exp false-exp)
                                   (append (free-vars-list test-exp) 
(free-vars-list true-exp) (free-vars-list false-exp)))
                           (begin-exp (exp1 exps) (append 
(free-vars-list exp1) (free-vars-list exps)))
                           (let-exp (ids rands body) (append 
(free-vars-list body)))
                           (proc-exp (ids body) (append (free-vars-list 
body)))
                           (app-exp (rator rands) (append 
(free-vars-list rator) (free-vars-list rands)))
                           (else '())
                        )
       )
     )
   )
)

(define closure
   (lambda (ids body env)

C:\Users\Todd\Documents\CS784\Assign2\3-5-mod.scm: 247:4
       (list (free-vars-list body))
   )
)

(define free-vars-list
   (lambda (body)

C:\Users\Todd\Documents\CS784\Assign2\3-5-mod.scm: 276:4
       (let ((freevars (difference todd ids)))
       (let ((saved-env
                    (extend-env
                          freevars
                          (map (lambda (v) (apply-env env v)) freevars)
                          (empty-env))
            ))
          (lambda (args)
                   (eval-expression body (extend-env ids args env)))
       )
     )
   ))
)

(run str2)

C:\Users\Todd\Documents\CS784\Assign2\3-5-mod.scm: 138:4
       (map (lambda (x) (eval-rand x env)) rands)))

C:\Users\Todd\Documents\CS784\Assign2\3-5-mod.scm: 120:8
           (let ((args (eval-rands rands env)))
           (eval-expression body (extend-env ids args env))))

Thanks for all your help!
Todd


Posted on the users mailing list.