question about apply

Notices and updates
Locked
cavva
Posts: 16
Joined: Sun Nov 11, 2007 2:45 am

question about apply

Post by cavva »

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

Here an examples to explain:

Code: Select all

(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

Code: Select all

(set 'my-func 'integer?)

(apply my-func (list 1))
=>true
but is this correct thing to do to get what i want?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

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

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

Code: Select 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
Posts: 16
Joined: Sun Nov 11, 2007 2:45 am

Post by cavva »

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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

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

Lutz

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

Locked