Path: ...!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown Newsgroups: comp.lang.c Subject: =?UTF-8?Q?Re=3A_technology_discussion_=E2=86=92_does_the_world_need?= =?UTF-8?B?IGEgIm5ldyIgQyA/?= Date: Wed, 10 Jul 2024 21:28:15 +0200 Organization: A noiseless patient Spider Lines: 77 Message-ID: References: <87h6d2uox5.fsf@nosuchdomain.example.com> <20240707164747.258@kylheku.com> <877cdur1z9.fsf@bsb.me.uk> <871q42qy33.fsf@bsb.me.uk> <87ed82p28y.fsf@bsb.me.uk> <87r0c1nzjj.fsf@bsb.me.uk> <86ikxd8czu.fsf@linuxsc.com> <20240710201454.0000527e@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 10 Jul 2024 21:28:17 +0200 (CEST) Injection-Info: dont-email.me; posting-host="c985da9317a18f8af2811d5dea43797f"; logging-data="2153764"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+zgNdw4rof0GGYjyu5EftB+rSIhpJT3c=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:n7iePWmvrs2x+yChhj5HWKxJmfo= In-Reply-To: <20240710201454.0000527e@yahoo.com> Content-Language: en-GB Bytes: 4212 On 10/07/2024 19:14, Michael S wrote: > On Wed, 10 Jul 2024 08:48:05 -0700 > Tim Rentsch wrote: > >> bart writes: >> >>> I earlier asked this: >>> >>> "So if arrays aren't passed by value in C, and they aren't passed >>> by reference, then how the hell ARE they passed?!" >> >> They aren't. C allows lots of things to be passed as an argument >> to a function: several varieties of numeric values, structs, >> unions, and pointers, including both pointers to object types and >> pointers to function types. C does not have a way for a function >> to take an argument that is either an array or a function. There >> is a way to take pointers to those things, but not the things >> themselves. Arrays and functions are second-class values in C. >> > > I'd like to see an example of the language that permits ahead-of-time > compilation and has functions as first-class values. > Haskell is the first the comes to mind for me, but you could pick any compiled functional programming language. I am by no means a Haskell expert, and I am not at all familiar with the way the language is compiled. But it is quite clear that it is an example of a language that has functions as first-class objects, and which is ahead-of-time compiled. The example below defines an int-to-int function "doubler", and also a function-to-function function "doTwice", and a function quadrupler that is defined as the result of applying the higher-order function doTwice to doubler. These are all compiled to assembly. module Example where doubler :: Int -> Int doubler x = 2 * x doTwice :: (Int -> Int) -> (Int -> Int) doTwice f x = f (f x) quadrupler = doTwice doubler shouldBeEighty = quadrupler 20 You can write much the same in C++ using lambdas (which are objects and can be passed around and returned as such) and templates (which are needed because the type of lambdas is hidden). Unfortunately, this also means that the functions don't get individually generated functions in assembly: auto doubler = [](int x) -> int { return 2 * x; }; auto doTwice = [](auto f) -> auto { return [f](int x) -> int { return f(f(x)); }; }; auto quadrupler = doTwice(doubler); auto shouldBeEiqhty = quadrupler(20);