2

We have two ways to define a function: defun and lambda, and we can use setf for labeling a lambda function.

(defun g (x) (* x x)) 
(setf f (lambda (x) (+ x x)))

The function can be the first element in a list.

(g 3)
9

Or it can be a parameter to the other function.

(mapcar #'g '(1 2 3))
(1 4 9)    

However with lambda, the usage is different.

(funcall f 3)
6

(mapcar f '(1 2 3))
(2 4 6)

I'm curious what's the logic behind the differences?

It's even more confusing compared with scheme's rather consistent use cases.

> (define (g x) (+ x x))
> (g 3)
6
> (map g '(1 3 4))
(2 6 8)

> (define f (lambda (x) (* x x)))
> (f 2)
4
> (map f '(1 2 3))
(1 4 9)
GoZoner
  • 67,920
  • 20
  • 95
  • 145
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • 3
    2 namespaces, one for var's like f and one for functions like g. you'd have to `funcall` `g` once it's been passed into map – daniel gratzer Jun 02 '13 at 02:36
  • Yes, Scheme is consistent in this case. – GoZoner Jun 02 '13 at 14:15
  • GoZoner: to claim thnat Scheme is consistent is religion, because it implies that anything else is inconsistent. Lisp is consistent in separating function and value namespaces, which extends in a consistent way to further namespaces. For instance, the existence of a `(defclass foo ..)` does not prevent you from having a function called `foo` or a lexical or dynamic variable called `foo`. – Kaz Jun 07 '13 at 23:28

1 Answers1

3

See Kent Pitman's paper Technical Issues of Separation in Function Cells and Value Cells

Barmar
  • 741,623
  • 53
  • 500
  • 612