help regarding lisp

Started by
1 comment, last by Vlion 19 years, 12 months ago
I''m trying to implement a basic genetic programming system in Lisp. The general goal is to have an end function f(x) with a predefined set of inputs mapping to outputs. My GP must come reasonably close to this. f must utilize a set of operators. What I''d like to do is in the initialization step randomly choose a set of operators, set them up in the test-f function, and run the test-set of criteria, and so forth. Now to the Lisp end of the problem. I''d like to do something like this:

(setq func-list (list 
   (defun add (a b) (+ a b)) 
   (defun sub (a b) (- a b))
  ...
))
 
Then somewherein my test-func I would have something along these lines: ((nth (rand-val-in-range 0 max) funclist) value) I don''t know how to get that to work. I keep getting told the first value is not a function name, which it really is... Arg. Any ideas on how to do this?
~V'lionBugle4d
Advertisement
I'm not sure of the proper technical terms, but put simply, Lisp doesn't evaluate the 'function place' in an expression. In order to use a function that is the result of some calculation, you need to use funcall or apply depending on your exact needs. You could also use eval, but it is probably more than you need and its use is generally frowned upon.

Also, if you aren't using the names of the functions, you might as well use anonymous functions something like
(setq func-list (list    (lambda (a b) (+ a b))    (lambda (a b) (- a b))    ...))  
that way you don't needlessly fill the namespace with function names you never use.

[edited by - Extrarius on April 23, 2004 3:23:20 AM]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
excellent !

Thanks for the advice! :D
~V'lionBugle4d

This topic is closed to new replies.

Advertisement