473,614 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C closures & lexical scoping

Woah... is it just me or do C programmers don't bother talking about
how cool C can be (compared to Lisp, Haskell, etc.) - functionally
speaking?

// Lexical scoping - via nested functions
#include <stdio.h>

int main() {
int x = 10;

// lexical scoping
static void test() {
x = 123;
}
test();

printf("%d", x);
return 0;
}

// Closures
#include <stdio.h>

int triple(int num) {
return num * 3;
}

int square(int num) {
return num * num;
}

int main() {
int x = 10;

int (*func)(int);

func = square;
printf("%d\n", func(5));

func = triple;
printf("%d\n", triple(5));

return 0;
}

Chris
Dec 12 '07 #1
14 2169
In article <6e************ *************** *******@e6g2000 prf.googlegroup s.com>,
Khookie <ch********@gma il.comwrote:
>Woah... is it just me or do C programmers don't bother talking about
how cool C can be (compared to Lisp, Haskell, etc.) - functionally
speaking?
Well...
>// Lexical scoping - via nested functions
Oops, C doesn't have nested functions. Gcc has them as an extension.
>// Closures
#include <stdio.h>

int triple(int num) {
return num * 3;
}

int square(int num) {
return num * num;
}

int main() {
int x = 10;

int (*func)(int);

func = square;
printf("%d\n", func(5));

func = triple;
printf("%d\n", triple(5));

return 0;
}
I don't see any closures there, just pointers to functions. Without
nested functions, there's nothing interesting to close over.

-- Richard
--
:wq
Dec 12 '07 #2
Philip Potter said:
jacob navia wrote:
<snip>
>>
Yeah, for instance
long long a,b,c;
...

c = a/b;

This generates a function call in C due to operator overloading...
Division is overloaded and works with long long types, but in a
32 bit system that means a function call

Not necessarily. It could easily mean an idiomatic sequence of machine
instructions (similar in concept to a C macro) instead.
Or it could simply be translated to a native division instruction, on
machines that have such a thing. Nothing in the rules prevents this.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dec 12 '07 #3
>>>>"K" == Khookie <ch********@gma il.comwrites:

KWoah... is it just me or do C programmers don't bother talking
Kabout how cool C can be (compared to Lisp, Haskell, etc.) -
Kfunctionally speaking?

Because we've seen what happens in comp.lang.lisp, which is all about
how cool Lisp is and how misunderstood Lisp programmers are, and how
ANY DAY NOW the great unwashed masses are FINALLY going to realize
just how cool Lisp is and abandon all other programming languages.

Scarcely a parenthesis to be found.

And we're spending so much time repeatedly explaining to Jacob Navia
the concept of "topicality ," and how he'd get better responses to his
posts if he posted them where they were on topic, that we just can't
muster the energy for any more foolish advocacy.

Charlton
--
Charlton Wilbur
cw*****@chromat ico.net
Dec 12 '07 #4
Richard Heathfield wrote:
Philip Potter said:
>jacob navia wrote:
<snip>
>>Yeah, for instance
long long a,b,c;
...

c = a/b;

This generates a function call in C due to operator overloading...
Division is overloaded and works with long long types, but in a
32 bit system that means a function call
Not necessarily. It could easily mean an idiomatic sequence of machine
instructions (similar in concept to a C macro) instead.

Or it could simply be translated to a native division instruction, on
machines that have such a thing. Nothing in the rules prevents this.
The phrase "32 bit system" is a woolly one, but I took it to imply that
the machine did not have a long long-sized division instruction.

Phil
Dec 12 '07 #5
Philip Potter wrote:
Richard Heathfield wrote:
>Philip Potter said:
>>jacob navia wrote:
<snip>
>>>Yeah, for instance
long long a,b,c;
...

c = a/b;

This generates a function call in C due to operator overloading...
Division is overloaded and works with long long types, but in a
32 bit system that means a function call
Not necessarily. It could easily mean an idiomatic sequence of machine
instruction s (similar in concept to a C macro) instead.

Or it could simply be translated to a native division instruction, on
machines that have such a thing. Nothing in the rules prevents this.

The phrase "32 bit system" is a woolly one, but I took it to imply that
the machine did not have a long long-sized division instruction.

Phil
I just wanted to point out the basic unity between operators and
functions. An operator can lead to a function call when an operation
is absent in the real machine. In a 32 bit system precisely most of
the time there is no 64 bit division. (Actually I have never seen a 32
bit system with native 64 bit integer division...)

Of course it can be inlined.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Dec 12 '07 #6
Charlton Wilbur wrote:
>>>>>"K" == Khookie <ch********@gma il.comwrites:

KWoah... is it just me or do C programmers don't bother talking
Kabout how cool C can be (compared to Lisp, Haskell, etc.) -
Kfunctionally speaking?

Because we've seen what happens in comp.lang.lisp, which is all about
how cool Lisp is and how misunderstood Lisp programmers are, and how
ANY DAY NOW the great unwashed masses are FINALLY going to realize
just how cool Lisp is and abandon all other programming languages.

Scarcely a parenthesis to be found.

And we're spending so much time repeatedly explaining to Jacob Navia
the concept of "topicality ," and how he'd get better responses to his
posts if he posted them where they were on topic, that we just can't
muster the energy for any more foolish advocacy.

Charlton

Yes, you are spending way too much time bashing navia as it seems.

You would better keep quiet then, and apply your own advice.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Dec 12 '07 #7
Charlton Wilbur <cw*****@chroma tico.netwrites:
>>>>>"K" == Khookie <ch********@gma il.comwrites:

KWoah... is it just me or do C programmers don't bother talking
Kabout how cool C can be (compared to Lisp, Haskell, etc.) -
Kfunctionally speaking?

Because we've seen what happens in comp.lang.lisp, which is all about
how cool Lisp is and how misunderstood Lisp programmers are, and how
ANY DAY NOW the great unwashed masses are FINALLY going to realize
just how cool Lisp is and abandon all other programming languages.

Scarcely a parenthesis to be found.

And we're spending so much time repeatedly explaining to Jacob Navia
Who is "we"? The royal "we"?
Dec 12 '07 #8
jacob navia wrote:
Philip Potter wrote:
>Richard Heathfield wrote:
>>Philip Potter said:
jacob navia wrote:
Yeah, for instance
long long a,b,c;
...
>
c = a/b;
>
This generates a function call in C due to operator overloading...
Division is overloaded and works with long long types, but in a
32 bit system that means a function call
Not necessarily. It could easily mean an idiomatic sequence of machine
instructio ns (similar in concept to a C macro) instead.
Or it could simply be translated to a native division instruction, on
machines that have such a thing. Nothing in the rules prevents this.
The phrase "32 bit system" is a woolly one, but I took it to imply that
the machine did not have a long long-sized division instruction.

I just wanted to point out the basic unity between operators and
functions. An operator can lead to a function call when an operation
is absent in the real machine. In a 32 bit system precisely most of
the time there is no 64 bit division. (Actually I have never seen a 32
bit system with native 64 bit integer division...)

Of course it can be inlined.
In which case why make it a function call in the first place? The thread
about the comparison between operators and function calls is over.
Dec 12 '07 #9
On Dec 12, 2:28 am, Khookie <chris.k...@gma il.comwrote:
Woah... is it just me or do C programmers don't bother talking about
how cool C can be (compared to Lisp, Haskell, etc.) - functionally
speaking?
The set of C programmers who

- are interested in Lisp and Haskell, but
- seriously misunderstand lexical closures, and who
- don't know what is or isn't a GCC extension

is probably vanishingly small.
// Lexical scoping - via nested functions
#include <stdio.h>

int main() {
int x = 10;

// lexical scoping
static void test() {
x = 123;
}
Function nesting is a GCC extension, not standard C.

More importantly, it doesn't implement first class lexical closures.
It has ``downward funargs'' only, like Pascal.

True lexical closures means being able to do something like this:

static int (*test(void))(v oid)
{
int local = 42;
int nested(void)
{
return local;
}

return &nested;
}

int main(void)
{
int (*func)(void) = test();
printf("%d\n", func());
return 0;
}

The output is 42, because in this imaginary C dialect, &nested creates
a closure instead of just a regular function pointer. A closure is an
object which contains not just the address of the function, but also
the function's captured lexical environment in which the ``int local =
42'' binding is in effect. That environment survives the termination
of the test() call, which is why when we call func(), it returns the
value 42.

The above is not permitted by the GCC extension. The GCC manual says
that ``all hell will break loose''.
Dec 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2299
by: Matt Knepley | last post by:
I must be misunderstanding how Python 2.3 handles lexical scoping. Here is a sample piece of code: def run(): a = 1 def run2(b): print a run2(2) print a run()
14
1558
by: Alexander May | last post by:
When I define a function in the body of a loop, why doesn't the function "close" on the loop vairable? See example below. Thanks, Alex C:\Documents and Settings\Alexander May>python Python 2.3.3 (#51, Dec 18 2003, 20:22:39) on win32 Type "help", "copyright", "credits" or "license" for more information.
5
1716
by: paolo veronelli | last post by:
I've a vague idea of the differences,I don't know scheme anyway. I'd like to see an example to show what is missing in python about closures and possibly understand if ruby is better in this sense. Iuse ruby and python in parallel for my job just to learn them and their differences and python is shorter and cleaner ,but i feel it's missing something, in closures.Any hints?
76
3754
by: Nick Coghlan | last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped in favour of a different syntax, rather than completely losing the ability to have anonymous functions. Anyway, I'm looking for feedback on a def-based syntax that came up in a recent c.l.p discussion:...
18
1788
by: jslowery | last post by:
I am not completely knowledgable about the status of lexical scoping in Python, but it was my understanding that this was added in a long time ago around python2.1-python2.2 I am using python2.4 and the following code throws a "status variable" not found in the inner-most function, even when I try to "global" it. def collect(fields, reducer): def rule(record): status = True
2
1561
by: Jon Harrop | last post by:
Just debating somewhere else whether or not Python might be considered a functional programming language. Lua, Ruby and Perl all seem to provide first class lexical closures. What is the current state of affairs in Python? Last time I looked they were just removing (?!) closures... -- Dr Jon D Harrop, Flying Frog Consultancy http://www.ffconsultancy.com/products/?u
2
1246
by: Fernando Perez | last post by:
Hi all, consider the following small example: """ Small test to try to understand a strange subtlety with closures """ def outer(nmax):
3
8788
by: globalrev | last post by:
i cant figure outif python has lexical or general scope. it seems functions have lexical scope but with some restrictions and some non-function scopes are dynamic?
26
2798
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
8182
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8130
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7093
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4052
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.