Todd

Thanks, you are definitely making me think, but I don't see what would be wrong with what I am trying. The first thing I tried was to remove free-vars and just call free-vars-list directly and change the case of

(var-exp (id) id)

to
(var-exp (id) (list id)) ;; in this example id == x

shouldn't it return '(x)? It always returns (x). If I try (list 'id) I 
obviously get '(id). Any clarification on why I would get (x) from (list id)?

Thanks for all your help!
Todd D



On 5/17/2011 9:20 PM, Todd O'Bryan wrote:
I apologize in advance for not actually reading through your code
beyond the point where it began using constructs that I'm not familiar
with, but I will give some advice that may or may not be helpful.

You're trying to return '(x). What you're actually returning is
(x)--an application of the value x with no arguments. It looks to me
like you're missing a level of quote-ness somewhere, or you're
accidentally evaluating the return value instead of just returning it.

Todd (but not the same one who asked the question)

On Tue, May 17, 2011 at 8:03 PM, Todd Dobmeyer<dobmeye...@wright.edu>  wrote:
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
_________________________________________________
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

_________________________________________________
 For list-related administrative tasks:
 http://lists.racket-lang.org/listinfo/users

Reply via email to