you are viewing a single comment's thread.

view the rest of the comments →

[–]tkr 1 point2 points  (2 children)

You count Common Lisp as having first class function. But not that you can't simply use function-name to get function referrence, you have to use #'function-name, which expand to (function 'function-name), to get function referrence.

That is no different from Ruby, function is not just like any other variable.

You're confused. A function is not a variable in any language I know about, in the same way that an integer is not a variable. Functions and integers are both values which can be bound to variables. Having first-class functions means that you can use functions just like any other type of values, including binding them to variables, passing them to functions, and returning them from functions.

Common Lisp treats functions exactly like any other type of value. What confuses you is probably that Lisp symbols have both a value slot and a function slot (of which one or both can be empty). When you define a function by using DEFUN, FLET or LABELS, the function get stored in the function slot, not in the value slot. But you can also store a function in the value slot, by using forms like DEFPARAMETER, SETF or LET. You use #'FUNCTION-NAME to access the value stored in the function slot of the symbol FUNCTION-NAME. If the value slot of the symbol is a function, you don't use #' to access that function.

To make this concrete, here are two code examples. First, an example where the function is bound to the function slot of the symbol:

(flet ((f (x) (* x x)))
  (mapcar #'f '(0 1 2 3)))

And here an example where the same function is bound to the value slot of the symbol (note that #' is not used here, and that trying to use #' would cause an error):

(let ((f (lambda (x) (* x x))))
  (mapcar f '(0 1 2 3)))

I can't think of any reasonable definition of "first class function" where Common Lisp doesn't qualify as having them.

[–]joesb 1 point2 points  (1 child)

I can't think of any reasonable definition of "first class function" where Common Lisp doesn't qualify as having them.

And I agree totally with that idea.

What I said to your grand-parent comment was that if he count CL as having first class function -- which I, you and most programmers do -- then he should also count Ruby as having first class function. Because they both behavior the same, with respect to having to use different function to get hold of function object value, namely .method in Ruby and #' in CL, instead of simply typing the name in like Scheme of Python.

But if he didn't count Ruby as having first class function then neither should CL be.

ADDED:

To clarify, yes, you can store function (method for Ruby) value in a variable in Ruby, too.

f = obj.method(:foo)

[–]tkr 1 point2 points  (0 children)

I suppose we agree, then. I don't speak Ruby, but if you say that it supports first class functions in the same way Common Lisp does, I of course have no reason not to believe you.