question about apply

Started by cavva, December 01, 2007, 08:35:48 AM

Previous topic - Next topic

cavva

How can i "call" a function and get back the result?



Here an examples to explain:



(set 'my-func 'integer?)

(apply my-func 1)
=> nil


How can i "apply" my-func and get back the return value? (true in this case)



I've found that this works

(set 'my-func 'integer?)

(apply my-func (list 1))
=>true


but is this correct thing to do to get what i want?

Lutz

#1
... yes, 'apply' takes the parameters of the function applied, in a list:



http://newlisp.org/downloads/newlisp_manual.html#apply">http://newlisp.org/downloads/newlisp_manual.html#apply



But in your case 'apply' is not needed at all:


(set 'my-func integer?)

(my-func 123) => true

(my-func "abc") => nil


Note that in the 'set' statement the symbol: integer? is not quoted. This dindn't matter for the apply becuase apply does evaluate the to apply first.



Lutz



ps: the new, renamed function is just as fast as the old one, and you can do the same thing with user-defined functions too.

cavva

#2
Thanks



I'm reading a book about Lisp, and the examples are all for Common Lisp;

In the code they pass a function as a paramter, then use "apply" to "call" the function ...



but you're right, i don't need apply



Edit:



they use funcall, not apply

Lutz

#3
If it comes to function application newLISP behaves more like Scheme, which also uses the same namespace for functions and variables(*). I don't recommend using a Common Lisp book (or any other LISP book) to learn newLISP. There are important differences and the style  of programming is different too.



If you are new to LISP, look at the Tutorials on the newLISP documentation page here:



http://newlisp.org/index.cgi?Documentation">http://newlisp.org/index.cgi?Documentation



it has a section with tutorials and another section with training videos.



Lutz



(*) and evaluates the functor part of an expression before applying it.