Question

I've searched the internet for the answers but haven't had much luck so please forgive me if this is a repeat question. I'll state my questions:= (?) thus and in the comments within the code that follows as well. (I'm using Emacs with clisp and Slime):

    ;; is this called nested function or partial application or what(?)

    (defun create-function(a)
        (defun add-function(x)(+ x a)))

    ->(create-function 8)
    ->ADD-FUNCTION
    ->(add-function 3)
    ->11

I can see the benefits of chaining these 'partial applications' but this isn't currying per se, right(?) Okay, lets check if my observations are correct:

    ;; the 'let statement *binds* 'loc to the 'clos object with 10 being the argument
    ;; for the formal parameter x. when funcall is applied to 'loc with 20 for y, the
    ;; lambda expression substitutions are complete and 200 is returned(?). 

    (defun close(x)
        (lambda(y)(* x y)))
    ->CLOSE
    (let ((loc (close 10)))
        (funcall loc 20))
    ->200

The above code is a closure(?) because of the scope of 'loc in the 'let statement: if 'clos was a mutable variable it would only change value while inside the 'let statement(?)... I think. Lastly if anyone could tell me how to get the step macro to play nice with the above functions, that would help a lot, (they evaluate immediately...) Thanks.

Was it helpful?

Solution

The first example can be called both nested function and partial evaluation. But the next example is more idiomatic. Both can be also called manual currying, although usually currying is understood as an automatic procedure. You can use currying in Lisp like this (one variant):

(defun curry (function &rest arguments)
  (lambda (&rest more)
    (multiple-value-call function
                         (values-list arguments) (values-list more))))

Speaking about the closure question: funcall under let is not a closure - a closure is a function definition inside let. Actually, your first example is a closure, because you define a function inside another function, and each function introduces something akin to a let binding.

Yet a more canonical example in your case would be:

(let ((z 10))
  (defun close (x)
    (* x z))
->CLOSE
(close 20)
->200

Here, z is a variable captured in a closure.

OTHER TIPS

The first example is neither a nested function or partial application. Instead it evaluates the inner defun, redefining the globally visible add-function, each time that the outer function is called. I don't think there's any particular name for that pattern.

Common Lisp does have lexically scoped nested functions with the flet and labels forms.

In the second example, the function close returns a closure (a function value that refers to variables lexically bound outside the body of that function). What you do with that return value does not affect what kind of value it is.

step will be pretty useless in SBCL unless you ask for extra debugging info. Do that by placing this form before the function:

(proclaim '(optimize (debug 3)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top