newLISP Fan Club

Forum => newLISP in the real world => Topic started by: borisT on January 30, 2019, 07:45:32 AM

Title: list of functions question
Post by: borisT on January 30, 2019, 07:45:32 AM
I'm a newbie, I'm trying to write some code that will select and run a function from a list of functions

Typically I'm doing something like this:


(define (myfunc) (println "this is myfunc"))

;; define a list of functions
(set 'funclist '(myfunc myfunc myfunc myfunc myfunc))

;; get and run the nth = 2 item of the list
(println "getting func")
(set 'afunc (funclist 2))
(println "calling func ")

(afunc)


Whatever I try I usully get the following error message:
Quotegetting func

calling func



ERR: invalid function : (afunc)


Could anyone explain what I'm, doing wrong?

Thanks.
Title: Re: list of functions question
Post by: rrq on January 30, 2019, 04:50:37 PM
(funclist 2) is the symbol myfunc, and not its "value", which is the function.

Thus, you would need to use (set 'afunc (eval (funclist 2))) so as to make afunc be a copy of the function named by the (funclist 2) symbol.
Title: Re: list of functions question
Post by: borisT on January 31, 2019, 12:39:01 AM
Makes sense. Thanks for that.
Title: Re: list of functions question
Post by: newBert on March 04, 2019, 04:18:01 AM
(With some delay...)

I think we could also write: (set 'funclist (list myfunc myfunc myfunc myfunc myfunc)), so we don't need 'eval' in (set 'afunc (funclist 2))