Why no closures?

Pondering the philosophy behind the language
Locked
Ishpeck
Posts: 14
Joined: Thu Jun 09, 2011 3:53 am

Why no closures?

Post by Ishpeck »

I'm really quite surprised that this isn't in the FAQ.

Why don't we have closures now?

Code: Select all

> (define (foo x) (lambda (y) (+ x y)))
(lambda (x) (lambda (y) (+ x y)))
> ((foo 3) 4)

ERR: value expected in function + : x
I can do this in Common Lisp:

Code: Select all

[16]> (defun foo (x) (lambda (y) (+ x y)))
FOO
[17]> (apply (foo 3) '(5))
8

Was this part of the design philosophy or have we just not done it yet?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Why no closures?

Post by Lutz »


TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: Why no closures?

Post by TedWalther »

I'm kind of surprised that example doesn't work. I know newLISP is dynamically scoped; I assumed that would work even in a dynamically scoped LISP? Not sure how closures come into it. Perhaps I better buckle down and read through SICP.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Why no closures?

Post by rickyboy »

No need to go to SICP. When (foo 3) gets evaluated in newLISP, the value of x is gone from the stack (it gets popped off), and without the environment carried by a closure to remember it, it gets forgotten. So then the value of (foo 3) (with the lost value of x) gets applied to the argument 4 and the evaluator complains that there is no x. We might not like this, but it does work as advertized.

For that example to work as the fellow intended, it needs to have the binding of x hang around in an environment (part of the closure) -- if only for a moment. However, in newLISP, what you want to do is just have any reference to x expanded on the fly. If this can be done, then in this case, there is no need to have environments hang around.

Code: Select all

> (define (foo* x) (letex (x x) (lambda (y) (+ x y))))
> ((foo* 3) 4)
7
;; Here's what's really happening with (foo* 3):
> (foo* 3)
(lambda (y) (+ 3 y))
;; Here is what happens with (foo 3):
> (foo 3)
(lambda (y) (+ x y))
;; The following works, because the value of x is still on the stack:
> ((lambda (x) ((lambda (y) (+ x y)) 4)) 3)
7
Of course, closures are neat for other reasons, but in this case, it is no great loss (or any loss), since all you really need to do is just "fill in" the value of x on the fly -- a way for doing that in newLISP is to expand the reference to x in that lambda expression on the fly.

P. S. -- And BTW, having closures will still not make me look sexy in my jeans. (Little board spam reference there. :)
(λx. x x) (λx. x x)

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: Why no closures?

Post by TedWalther »

In some sense, a (let ...) block is a closure. I assumed that (fn ...) acted as a closure in the same way, like a let block. Thanks for the explanation, ricky.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

Ishpeck
Posts: 14
Joined: Thu Jun 09, 2011 3:53 am

Re: Why no closures?

Post by Ishpeck »

It's good to see that I'm the one who's the moron.

Thanks for the help, all.

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: Why no closures?

Post by xytroxon »

Lisp(s) make all mere mortals "morons" at some point...

-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

Camryn65
Posts: 3
Joined: Thu Jan 05, 2012 9:27 am

Re: Why no closures?

Post by Camryn65 »

I am curious of why these wack jobs can not have a decent bone in there body and give us NON'S a sence of closure and at least say our goodbyes in a Mature manor.

I mean What the heck we shared so much together and just to vanish into thin air..Thats just f------g wrong and it en rages me.

What kind of people are they?

kgxs65633
Posts: 1
Joined: Mon Mar 13, 2023 5:39 am

Re: Why no closures?

Post by kgxs65633 »

However, there are a great number of issues that arise in the minds of customers while they are in the process of hiring Phuket mature escorts. Regarding these questions, we have provided further information.

Locked