1

I'm trying to figure out how Common Lisp deals with functions. If I do something like

(defun square (n)
  (* n n))

I can later call, for example, (square 10). However, if I do something like

(defun reapply (fun)
  (lambda (n)
    (funcall fun (funcall fun n))))

and then define

(defparameter to-the-fourth (reapply #'square))

the function to-the-fourth can not be called as (to-the-fourth 10); I instead have to write (funcall to-the-fourth 10). I think that's because defparameter defines variables in the variable namespace rather than the function namespace. Is there any way to define a function in the function namespace? The following seems pretty verbose:

(defun to-the-fourth (a)
  (funcall (reapply #'square) a))
4
  • (defvar square (n) (* n n)) is not valid Common Lisp code. There is also no , in Common Lisp at the end of Lisp forms. Commented Aug 14, 2021 at 20:25
  • Oops, fixed that. Commented Aug 14, 2021 at 20:54
  • It would be a good idea to check your code first, for example by trying it. The second defun form lacks closing parentheses and (n) in the function body is not valid code, since n is not a function. Then (reapply square) also does not work, since the variable square is undefined. Commented Aug 14, 2021 at 20:58
  • Ugh sorry, should all work now. Commented Aug 14, 2021 at 21:28

2 Answers 2

3

First, two typos:

(defun reapply (fun)
  (lambda (n) (funcall fun (funcall fun (n)))

should be

(defun reapply (fun)
  (lambda (n) (funcall fun (funcall fun n))))

and

(defparameter to-the-fourth (reapply square))

should be

(defparameter to-the-fourth (reapply #'square))

(Or, *to-the-fourth*, as it is common to name special variables with asterisks on both sides.) Here the #' shows that you want the function associated with the symbol.

What you ask for is

(setf (symbol-function 'to-the-fourth) (reapply #'square))

This is how you can edit the function associated with a symbol directly. But it is not very good style.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh nice, thanks! What would the proper style be for working with higher-order functions?
I'd stay with funcalls. It is a bit verbose, but that's the price to pay for a Lisp-2, where variables and functions are separated. Scheme's syntax is nicer in this regard. Note also that while CL does have functional elements, idiomatic code is much less functional in CL than in Scheme. So probably I woudn't write something like this at all :)
3

One of the differences between your version and vukung's is late binding:

CL-USER 1 > (defun square (n)
              (* n n))
SQUARE

CL-USER 2 > (defun reapply (fun)
              (lambda (n)
                (funcall fun (funcall fun n))))
REAPPLY

CL-USER 3 > (defun to-the-fourth (a)
              (funcall (reapply #'square) a))
TO-THE-FOURTH

CL-USER 4 > (to-the-fourth 10)
10000

CL-USER 5 > (defun square (n)
              (* n n 2))
SQUARE

CL-USER 6 > (to-the-fourth 10)
80000

I this case the function to-the-fourth calls the current definition of square. This is usually the case -> unless an optimizing compiler is inlining the function.

Compare that with the version where we deal with function objects:

CL-USER 7 > (setf (symbol-function 'to-the-fourth)
                  (reapply #'square))
#<anonymous interpreted function 70D00015DC>

CL-USER 8 > (to-the-fourth 10)
80000

CL-USER 9 > (defun square (n)
              (* n n))
SQUARE

CL-USER 10 > (to-the-fourth 10)
80000

This one calls always the version of definition time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.