#lang racket ; returns true if v is a variable (not a number) not in vars (define (is-new-var? v vars) (and (not (number? v)) (not (member v vars)))) ; extracts out a list of all the variables in E. Each variable appears ; exactly once in the result list (define (exp-vars E) ; given an current expression Ec, and a list vars of variables seen ; so far, add and new variables of Ec (define (exp-vars-r Ec vars) (cond ; atoms are themselves, so if not seen already, add a non-number ; to the list of variables [ (not (list? Ec)) (if (is-new-var? Ec vars) (cons Ec vars) vars) ] ; single element lists are the value of their first element, so ; get variables in the expression given by the first element. [ (null? (rest Ec)) (exp-vars-r (first Ec) vars) ] ; binary expressions, update the variables with the ones on the ; left, and then update those with the ones on the right. Note ; the threading of vars through the recursion. [ else (exp-vars-r (third Ec) (exp-vars-r (first Ec) vars) ) ] )) (exp-vars-r E '()) )