Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <5034A7BB.10006@westcontrol.removethisbit.com> (permalink) |
|---|---|
| Date | 2012-08-22 11:34 +0200 |
| From | David Brown <david@westcontrol.removethisbit.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: Function factories |
| References | <k10rgf$oep$1@speranza.aioe.org> <EJqdnfZqgq8B4KnNnZ2dnUVZ8oqdnZ2d@lyse.net> <158866c9-ecb4-4242-b62e-ce217fbd16d9@q17g2000yqh.googlegroups.com> |
On 22/08/2012 10:52, Nick Keighley wrote:
> On Aug 22, 7:34 am, David Brown <da...@westcontrol.removethisbit.com>
> wrote:
>> On 21/08/2012 22:36, Edward Rutherford wrote:
>>
>>
>>
>>
>>
>>> Hello
>>
>>> I want to make a factory that produces other functions according to a
>>> parameter.
>>
>>> The following code does not compile:
>>
>>> typedef int *(foo)(int);
>>
>>> foo adder_factory(int x)
>>> {
>>> int f(int a)
>>> {
>>> return a + x;
>>> }
>>> return &f;
>>> }
>>
>>> The idea is that adder_factor(42) should be a function that adds 42 to
>>> whatever it gets passed.
>>
>>> What's the right way of doing this in C? How can I get closures or
>>> something similar in C?
>>
>> You can't.
>>
>> You can get sort-of simulated closures in complicated and verbose ways,
>> but there is no good way to do it. gcc supports nested functions as an
>> extension to C, but that won't give you closures like this.
>>
>> If you want to stick close to C, then C++11 supports lambda functions
>> and closures.
>>
>> If you want to write code that makes strong use of closures in the
>> style, then use a programming language that supports it. The latest C++
>> standard will get you the basics, but if you want to write code in a
>> more functional programming style, there are much better languages to
>> work with (ocaml, python, haskell, etc.)
>
> scheme
>
> (and does Python have particularly good support for functional
> programming?)
>
Python has quite good support for functional programming styles, though
it is mainly a procedural / object oriented language. You do get
closures, list comprehension, functions as first-class objects, and a
sort of lazy evaluation (through iterators). It does not have pattern
matching in the way languages like Haskell have, which would have been nice.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-21 20:36 +0000
Re: Function factories Nobody <nobody@nowhere.com> - 2012-08-21 21:55 +0100
Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 16:36 +0000
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 13:01 -0400
Re: Function factories Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-22 18:33 +0000
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 22:26 -0400
Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 08:34 +0200
Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-22 01:52 -0700
Re: Function factories David Brown <david@westcontrol.removethisbit.com> - 2012-08-22 11:34 +0200
Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:30 +0100
Re: Function factories Gareth Owen <gwowen@gmail.com> - 2012-08-24 17:29 +0100
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-22 12:32 +0200
Re: Function factories tom st denis <tom@iahu.ca> - 2012-08-22 07:40 -0700
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-22 11:05 -0400
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 11:08 +0200
Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 13:27 +0100
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 11:13 -0400
Re: Function factories "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-23 18:18 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:40 -0400
Re: Function factories Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2012-08-23 09:29 -0700
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 18:44 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 12:46 -0400
Re: Function factories Chicken McNuggets <chicken@mcnuggets.com> - 2012-08-23 18:02 +0100
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 19:24 +0200
Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 18:07 -0700
Re: Function factories Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-23 21:30 -0400
Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 21:46 -0700
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 13:35 -0400
Re: Function factories jacob navia <jacob@spamsink.net> - 2012-08-23 20:13 +0200
Re: Function factories eq mail <eq.mail.id@gmail.com> - 2012-08-23 17:57 -0700
Re: Function factories Andrew Cooper <amc96@cam.ac.uk> - 2012-08-25 02:35 +0100
Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 21:21 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 15:36 -0400
Re: Function factories Melzzzzz <mel@zzzzz.invalid> - 2012-08-23 22:09 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 16:20 -0400
Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-23 22:33 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 17:07 -0400
Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 14:51 -0700
Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 00:09 +0200
Re: Function factories James Kuyper <jameskuyper@verizon.net> - 2012-08-23 19:01 -0400
Re: Function factories Keith Thompson <kst-u@mib.org> - 2012-08-23 16:31 -0700
Re: Function factories Melzzzzz <mel@zzzzz.com> - 2012-08-24 18:31 +0200
Re: Function factories Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-08-24 02:37 -0700
csiph-web