Closures
flag
Messages 1 - 10 of 24 - Collapse all
/groups/adfetch?adid=lRKInQ8AAABSHHt98qsIPj2G3VoXQl64
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
1.  H.  
View profile  
 More options Jan 15 2006, 2:45 am
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
own scope. Then I read the wikipedia definition of closure, and it said
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"
(http://www.le-hacker.org/papers/gobject/ch03.html)

When I think "callback", the example that comes to mind is one in
JavaScript. There is a built in sort method which has default
alphabetical behavior. However it can be modified to work in a
different way (e.g. numerically), or even on different types, like:

// Assume an array of Person objects might need to be sorted by age or
name
// That's easy. Just create two callbacks.
function ageCallback(a,b)
{
   return a.age - b.age;

}

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
has to do with closures!  And I'm getting kind of confused trying to
put all of these "closure" definitions together. Yeah, so any help
would be great.


 
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.
2.  Kjetil S. Matheussen  
View profile  
 More options Jan 15 2006, 4:20 am
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:
> 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!

Well, in Java and C++, an object is some kind of lame closure. If you have
closures in your languages, you probably can make objects as well.

By the way, according to this page: http://www.paulgraham.com/icad.html ,
javascript do support closures:

function foo(n) {
   return function (i) {
            return n += i } }

...which in scheme is:

(define (foo n)
    (lambda (i)
       (+ i n)))

In C, you could make a manual closure by doing something like this:

static int innerfunction(closure *c,int i){
    return c->n+i:

}

closure* foo(int n){
   closure* ret=calloc(1,sizeof(closure));
   closure->func=innerfunction;
   closure->n=n;
   return closure;

}
> At first, I kind of thought of a closure as a block of code with its
> own scope.

Hmm, yeah. Something like that.

> Then I read the wikipedia definition of closure, and it said
> that closures store information across function calls, so I thought of
> static variables in these blocks of code.

No, thats not what it means.

> 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.
3.  Kjetil S. Matheussen  
View profile  
 More options Jan 15 2006, 4:24 am
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:
> closure* foo(int n){
>  closure* ret=calloc(1,sizeof(closure));
>  closure->func=innerfunction;
>  closure->n=n;
>  return closure;
> }

Oops. Last line in that function should be "return ret;" :-)

--


 
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.
4.  Kjetil S. Matheussen  
View profile  
 More options Jan 15 2006, 4:30 am
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)
   (lambda (i)
     (set! n (+ n i))
     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.
5.  Kjetil S. Matheussen  
View profile  
 More options Jan 15 2006, 5:09 pm
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{
   int (*func)(struct _closure*,int);
   int n;

}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.
6.  H.  
View profile  
 More options Jan 16 2006, 3:35 pm
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:

> static int innerfunction(closure *c,int i){
>     return c->n+i:
> }

> closure* foo(int n){
>    closure* ret=calloc(1,sizeof(closure));
>    closure->func=innerfunction;
>    closure->n=n;
>    return closure;
> }

Hmm. It certainly seems much more of a hack job in C!

[totalSideQuery]
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]


 
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.
7.  Bruce Stephens  
View profile  
 More options Jan 16 2006, 6:27 pm
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

"H." <hbe...@gmail.com> writes:

[...]

> [totalSideQuery]
> 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]

Not especially.  Objects help, and in C++ one can overload the
function-calling operator.  So it's all more convenient in C++ than in
C:

class adder {
    int i;
public:
    adder(int n): i(n) {}
    operator()(int n) {return n+i;}

};

...

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,
ECMAscript, etc.


 
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.
Discussion subject changed to "C++ closure example" by H.
8.  H.  
View profile  
 More options Jan 16 2006, 9:21 pm
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
a valid one and I can get it to work.


 
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.
9.  Thant Tessman  
View profile  
 More options Jan 16 2006, 10:06 pm
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
C++ by leveraging templates and many other tricks, but they always fall
short of being satisfactory. Issues with memory management, argument
calling conventions, etc. never really go away in C++.

-thant

--
We have now sunk to a depth at which restatement of the obvious is the
first duty of intelligent men. -- George Orwell (attributed)


 
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.
10.  Bruce Stephens  
View profile  
 More options Jan 17 2006, 8:30 pm
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

"H." <hbe...@gmail.com> writes:

[...]

> When I try to compile this, I get an "ISO C++ forbids declaration of
> 'operator()' with no type" error.

Oops, sorry.  Should be int operator()..., obviously.

> It seems like a good example I'd like to play around with more, if
> it's a valid one and I can get it to work.

This kind of thing (creating objects which provide an operator()) is
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++
for some other reason (because you're maintaining a system already
written in it, for example), it can be useful to know what wacky
things are possible, even if you decide not to use many of them.


 
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