Search code examples
clisp

How to define a function that returns a function in clisp


Here is a function I defined in Scheme:

(define (multn n) (lambda (x) (* x n)))

and when I type

((multn 7) 5)

it gives 35.

However, when I used Clisp:

(defun multn (n) (lambda (x) (* x n)))

it gives me error: 'EVAL: (MULTN 7) is not a function name; try using a symbol instead'

How can I get it work? Thanks in advance.


Solution

  • You need to use funcall because of separate namespaces in Common Lisp:

    [1]> (defun multn (n) (lambda (x) (* x n)))
    MULTN
    [2]> (funcall (multn 7) 5)
    35
    

    See for example the Common Lisp cookbook for an in-depth explanation.