Newsgroups: comp.lang.scheme
From: "H." <hbe...@gmail.com>
Date: 14 Jan 2006 23:45:53 -0800
Local: Sun, Jan 15 2006 2:45 am
Subject: Closures
So I'm trying to get my head around exactly what a "closure" is. I've
read what SICP says about closures via all of the references listed in the index. I understand the general concept of closures, but not the full implications. Part of my problem is that I have a lot of other terms running through my head from other languages (namely, JavaScript, Java, C++), and they seem to be getting in the way of understanding, instead of facilitating it! At first, I kind of thought of a closure as a block of code with its When I think "callback", the example that comes to mind is one in // Assume an array of Person objects might need to be sorted by age or } function nameCallback(a,b) { if (name.a < name.b) return -1; else if (name.b > name.a) return 1; else return 0; } arrayOfPersonObjects.sort(ageCallback); // sort by age arrayOfPersonObjects.sort(nameCallback); // sort by name This is a nifty ability, I think. But, um, I can't figure out what it You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.scheme
From: "Kjetil S. Matheussen" <kje...@ccrma.stanford.edu>
Date: Sun, 15 Jan 2006 01:20:36 -0800
Local: Sun, Jan 15 2006 4:20 am
Subject: Re: Closures
On Sat, 14 Jan 2006, H. wrote: Well, in Java and C++, an object is some kind of lame closure. If you have > So I'm trying to get my head around exactly what a "closure" is. I've > read what SICP says about closures via all of the references listed in > the index. I understand the general concept of closures, but not the > full implications. > Part of my problem is that I have a lot of other > terms running through my head from other languages (namely, JavaScript, > Java, C++), and they seem to be getting in the way of understanding, > instead of facilitating it! closures in your languages, you probably can make objects as well. By the way, according to this page: http://www.paulgraham.com/icad.html , function foo(n) { ...which in scheme is: (define (foo n) In C, you could make a manual closure by doing something like this: static int innerfunction(closure *c,int i){ } closure* foo(int n){ closure* ret=calloc(1,sizeof(closure)); closure->func=innerfunction; closure->n=n; return closure; } Hmm, yeah. Something like that. > At first, I kind of thought of a closure as a block of code with its > own scope. > Then I read the wikipedia definition of closure, and it said No, thats not what it means. > that closures store information across function calls, so I thought of > static variables in these blocks of code. > Recently I read a closure was a "generic representation of a callback" That sounds like a very confusing definition... :-) -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.scheme
From: "Kjetil S. Matheussen" <kje...@ccrma.stanford.edu>
Date: Sun, 15 Jan 2006 01:24:47 -0800
Local: Sun, Jan 15 2006 4:24 am
Subject: Re: Closures
On Sun, 15 Jan 2006, Kjetil S. Matheussen wrote: Oops. Last line in that function should be "return ret;" :-) > closure* foo(int n){ > closure* ret=calloc(1,sizeof(closure)); > closure->func=innerfunction; > closure->n=n; > return closure; > } -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.scheme
From: "Kjetil S. Matheussen" <kje...@ccrma.stanford.edu>
Date: Sun, 15 Jan 2006 01:30:15 -0800
Local: Sun, Jan 15 2006 4:30 am
Subject: Re: Closures
And that last one should have been like this:
(define (foo n) Well, time for bed... -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.scheme
From: "Kjetil S. Matheussen" <kje...@ccrma.stanford.edu>
Date: Sun, 15 Jan 2006 14:09:32 -0800
Local: Sun, Jan 15 2006 5:09 pm
Subject: Re: Closures
Hmpf. Let me get this right: :-)
typedef struct _closure{ }closure; static int innerfunction(closure *c,int i){ c->n+=i; return c->n: } closure* foo(int n){ closure* ret=calloc(1,sizeof(closure)); ret->func=innerfunction; ret->n=n; return ret; } Or in scheme: (define (foo n) (lambda (i) (set! n (+ i n)) n)) -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.scheme
From: "H." <hbe...@gmail.com>
Date: 16 Jan 2006 12:35:57 -0800
Local: Mon, Jan 16 2006 3:35 pm
Subject: Re: Closures
> In C, you could make a manual closure by doing something like this: Hmm. It certainly seems much more of a hack job in C! > static int innerfunction(closure *c,int i){ > closure* foo(int n){ [totalSideQuery] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.scheme
From: Bruce Stephens <bruce+use...@cenderis.demon.co.uk>
Date: Mon, 16 Jan 2006 23:27:05 +0000
Local: Mon, Jan 16 2006 6:27 pm
Subject: Re: Closures
[...]
> [totalSideQuery] Not especially. Objects help, and in C++ one can overload the > Do you happen to know if any implementation details changed between C > and C++ that address closure issues ? I would think "no" because C is a > subset of C++, and I would think "yes" because C++ is object-oriented > and C is not.[/totalSideQuery] function-calling operator. So it's all more convenient in C++ than in C: class adder { }; ... adder plus_7(7); std::cout << plus_7(42) << std::endl << plus_7(18) << std::endl; Still clumsy compared to something that really has closures like Lisp, You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
| |||||||||||||
Newsgroups: comp.lang.scheme
From: "H." <hbe...@gmail.com>
Date: 16 Jan 2006 18:21:49 -0800
Local: Mon, Jan 16 2006 9:21 pm
Subject: C++ closure example
When I try to compile this, I get an "ISO C++ forbids declaration of
'operator()' with no type" error. It seems like a good example I'd like to play around with more, if it's You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.scheme
From: Thant Tessman <a...@standarddeviance.com>
Date: Mon, 16 Jan 2006 20:06:57 -0700
Local: Mon, Jan 16 2006 10:06 pm
Subject: Re: C++ closure example
The return type is missing. Without actually having tried to compile
something, I would guess it should be: int operator()(int n){return n+i;} You can actually build quite sophisticated function object systems in -thant -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.scheme
From: Bruce Stephens <bruce+use...@cenderis.demon.co.uk>
Date: Wed, 18 Jan 2006 01:30:43 +0000
Local: Tues, Jan 17 2006 8:30 pm
Subject: Re: C++ closure example
[...]
> When I try to compile this, I get an "ISO C++ forbids declaration of Oops, sorry. Should be int operator()..., obviously. > 'operator()' with no type" error. > It seems like a good example I'd like to play around with more, if This kind of thing (creating objects which provide an operator()) is > it's a valid one and I can get it to work. well known in C++, and there's some convenience functions in the standard library. More in boost, of course: <http://www.boost.org/libs/libraries.htm#Function-objects>. It's not really lisp, or scheme, or closures. But if you're using C++ You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
| Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy |
| ©2013 Google |