Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

help with dual namespaces in cl

Name: Anonymous 2009-12-23 23:42

When do I use function? funcall? apply? It seems you can't use function out of the box, which is a pain in the ass because I don't see why there should be a seperate step to higher-order function application.

Name: Anonymous 2009-12-24 0:07

You missed one MULTIPLE-VALUE-CALL. Personally I like the separate namespaces as they help a lot with macros, and allow me to use any argument names that I'd like. You've also forgotten about FLET and LABELS.
Read http://www.nhplace.com/kent/Papers/Technical-Issues.html for a better understanding of the issues.
#'fun == (function fun) == function named by symbol fun in the current environment
And you use apply in Scheme too.

Is it really a separate step? If you have an argument, and it contains a functional value, what do you think the following code would do:

(lambda (list) (list list))

In a Lisp-1(such as Scheme), it will call the function located in the list argument with itself, in a Lisp-2(such as CL), it will make a list of the first argument. If you really don't want all the advantages that Lisp-2 gives you, you could just use a code-walker which lets you write in Lisp-1 syntax, there's a few out there.

Name: Anonymous 2009-12-24 0:42

>>2
Of course, a schemer would use a more descriptive name than list ;) or the classic `lst'

Name: Anonymous 2009-12-24 1:37

>>2
Thanks, the document cleared things off.

Well, off to see your mother!

Name: Anonymous 2009-12-24 1:48

If funcall looks in the other namespace, why do we even need to pass functions in with the hash-aprostrophe?

Name: Anonymous 2009-12-24 2:30

>>5
funcall doesn't look into the other namespace.
funcall/apply are just functions.

(funcall fun arg1 arg2 ... argn) is just a normal function call, which means, fun and the arguments are evaluated in order, and their values(!) are passed to the funcall function. That funcall call would be valid if fun had a functional value by itself, like (let ((fun (lambda () 123))) (funcall fun))=>3
On the other hand, (let ((fun (lambda () 123))) (funcall #'fun)) would throw an error, unless there was a local (or non-local) function named fun. #'stuff  is just a dispatch macro character for (function stuff). (function stuff) would look up the symbol stuff in the local environment and return the functional value associated with that symbol (a symbol is actually a more complex object with a few fields, such as a name(string), home package, value and functional value).
In short, APPLY/FUNCALL are just functions which just evaluate their arguments in order, like all functions. You are passing values to any normal function call (not a macro or a special operator). If you'd like to pass a value from the functional namespace, you have to use #' or (function ...). (There are other ways to get the functional value of a symbol, such as symbol-function or fdefinition, but those are more complex topics, though it would probably make it simpler to understand). This is actually much simpler than I'm making it sound. One final consideration, the first element(the car of the form) is always taken from the functional namespace(or it's a macro or special form, but those topics are slightly more complex, so you can ignore them until you reach them), while the rest are just plain values.

P.S.: An example where the previous example would be valid with #' is

(flet ((fun () 123)) (funcall #'fun)) ;=>123

What do you think the result of this would be:

(flet ((fun () 123))
   (let ((fun #'(lambda () #'fun)))
     (funcall (funcall fun)))) ;=> [spoiler]123[/spoiler]


If you still have any questions, it will probably become clear when you learn symbols, packages and environments in more detail. I would also suggest you test everything in your implementation of choice and use a good inspector package along with Emacs+SLIME, it would surely clear up a lot of details about how things work under the hood and maybe give you an easier way to gain understanding if reading books or the standard is too cumbersome for you.

Name: >>6 2009-12-24 2:31

Damn BBCOED failure.

Name: >>6 2009-12-24 2:38

Here's another interesting thing that may clear up some things for you. You can write FUNCALL through APPLY, but not vice-versa:

(defun funcall (function &rest arguments)
  (apply function arguments))

(APPLY (function) itself can be implemented using the special operator MULTIPLE-VALUE-CALL, and MULTIPLE-VALUE-CALL could itself be implemented through APPLY if you could use MULTIPLE-VALUE-LIST in the code (which is a macro), however MULTIPLE-VALUE-LIST itself is traditionally implemented through a single MULTIPLE-VALUE-CALL call, and thus we have meta-circularity, which is why one of them would have to be a special operator. Due to efficiency concerns, MULTIPLE-VALUE-CALL is such an operator... I think I'm getting offtopic, sorry about that.)

Name: Anonymous 2009-12-24 3:35

My faith in /prog/ has been restored.

Name: Anonymous 2010-12-21 14:39

Name: Anonymous 2010-12-25 23:26

Don't change these.
Name: Email:
Entire Thread Thread List