list of functions question

Q&A's, tips, howto's

list of functions question

Postby borisT » Wed Jan 30, 2019 3:45 pm

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:

Code: Select all
(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:
getting func
calling func

ERR: invalid function : (afunc)


Could anyone explain what I'm, doing wrong?
Thanks.
borisT
 
Posts: 2
Joined: Wed Jan 30, 2019 8:39 am

Re: list of functions question

Postby ralph.ronnquist » Thu Jan 31, 2019 12:50 am

(funclist 2) is the symbol myfunc, and not its "value", which is the function.
Thus, you would need to use
Code: Select all
(set 'afunc (eval (funclist 2)))
so as to make afunc be a copy of the function named by the (funclist 2) symbol.
ralph.ronnquist
 
Posts: 202
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: list of functions question

Postby borisT » Thu Jan 31, 2019 8:39 am

Makes sense. Thanks for that.
borisT
 
Posts: 2
Joined: Wed Jan 30, 2019 8:39 am

Re: list of functions question

Postby newBert » Mon Mar 04, 2019 12:18 pm

(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))
BertrandnewLISP v.10.7.5 64-bit on Linux (Peppermint 9)
newBert
 
Posts: 137
Joined: Fri Oct 28, 2005 5:33 pm
Location: France


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 0 guests

cron