scheme - Convert lists into functions -
i have little noob question. have homework on genetic programming in scheme , first step finish given functions.
i got point have execute randomly generated function possible parameters in range (using map). "function" list '(* (+ 1 x) (- x (* 2 3)))
.
how can execute given parameter? (for example x = 2). way, generated function has maximum of 1 parameter (it's x or none).
thanks!
here's solution:
(define (execute expr) (lambda (x) (let recur ((expr expr)) (case expr ((x) x) ((+) +) ((-) -) ((*) *) ((/) /) (else (if (list? expr) (apply (recur (car expr)) (map recur (cdr expr))) expr))))))
example usage:
> (define foo (execute '(* (+ 1 x) (- x (* 2 3))))) > (foo 42) => 1548
Comments
Post a Comment