[racket] Code Error Creating List

From: Marco Morazan (morazanm at gmail.com)
Date: Wed May 18 13:37:35 EDT 2011


In addition to Matthias' observations:

> (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)))
> ...

There is some confusion here about what body is. Writing down a
contract, as done in EOPL, would be helpful to you and to those
reading your code. You are testing if body is a list or an expression.
Why? It should always be the case that body is an expression. In other
words, free-vars-list ought to be a function that processes
expressions and calling it with a list ought to be a mistake.

I also add the observation that not all vars may be free. Your code
seems to assume that when it is called with a var-exp.





Posted on the users mailing list.