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 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
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
>>>>"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
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
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
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
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"?
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.
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''. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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()
|
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.
|
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?
|
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:...
|
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
| |
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
|
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):
|
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?
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |