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

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -