Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

One Bell System - it used to work before they installed the Dimension!


devel / comp.lang.c / Recursion, Yo

SubjectAuthor
* Recursion, YoLawrence D'Oliveiro
+* Re: Recursion, Yofir
|`* Re: Recursion, YoJanis Papanagnou
| +* Re: Recursion, YoLawrence D'Oliveiro
| |+* Re: Recursion, YoJanis Papanagnou
| ||`* Re: Recursion, YoBen Bacarisse
| || `* Re: Recursion, YoJanis Papanagnou
| ||  `* Re: Recursion, YoKeith Thompson
| ||   `- Re: Recursion, YoJanis Papanagnou
| |`- Re: Recursion, Yobart
| `* Re: Recursion, YoBen Bacarisse
|  +- Re: Recursion, YoBen Bacarisse
|  `* Re: Recursion, YoLawrence D'Oliveiro
|   +- Re: Recursion, YoChris M. Thomasson
|   +* Re: Recursion, YoDavid Brown
|   |+* Re: Recursion, YoLawrence D'Oliveiro
|   ||`* Re: Recursion, YoDavid Brown
|   || +* Re: Recursion, Yobart
|   || |+* Re: Recursion, YoDavid Brown
|   || ||`* Re: Recursion, YoLawrence D'Oliveiro
|   || || +* Re: Recursion, YoKaz Kylheku
|   || || |`* Heh heh... (Was: Recursion, Yo)Kenny McCormack
|   || || | `* Re: Heh heh... (Was: Recursion, Yo)Kaz Kylheku
|   || || |  `- Re: Heh heh... (Was: Recursion, Yo)Kenny McCormack
|   || || `* Re: Recursion, YoDavid Brown
|   || ||  +* Re: Recursion, YoScott Lurndal
|   || ||  |`* Re: Recursion, YoKaz Kylheku
|   || ||  | +* Re: Recursion, YoScott Lurndal
|   || ||  | |`* Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | | +* Re: Recursion, YoKaz Kylheku
|   || ||  | | |`- Re: Recursion, YoDan Cross
|   || ||  | | `* Re: Recursion, YoJanis Papanagnou
|   || ||  | |  +* Re: Recursion, YoDavid Brown
|   || ||  | |  |`* Re: Recursion, YoJanis Papanagnou
|   || ||  | |  | +* Re: Recursion, YoDavid Brown
|   || ||  | |  | |`* Re: Recursion, YoJanis Papanagnou
|   || ||  | |  | | `- Re: Recursion, YoDavid Brown
|   || ||  | |  | `- Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | |  +* Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | |  |`* Re: Recursion, YoJanis Papanagnou
|   || ||  | |  | +- Re: Recursion, Yobart
|   || ||  | |  | `* Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | |  |  +* Re: Recursion, YoMichael S
|   || ||  | |  |  |+* Re: Recursion, YoBen Bacarisse
|   || ||  | |  |  ||`* Re: Recursion, YoMichael S
|   || ||  | |  |  || `* Re: Recursion, YoJanis Papanagnou
|   || ||  | |  |  ||  `* Re: Recursion, YoKeith Thompson
|   || ||  | |  |  ||   `* Re: Recursion, YoBen Bacarisse
|   || ||  | |  |  ||    `* Re: Recursion, YoKeith Thompson
|   || ||  | |  |  ||     +* Re: Recursion, Yobart
|   || ||  | |  |  ||     |`- Re: Recursion, YoBen Bacarisse
|   || ||  | |  |  ||     `* Re: Recursion, YoJanis Papanagnou
|   || ||  | |  |  ||      +- Re: Recursion, YoJanis Papanagnou
|   || ||  | |  |  ||      `- Re: Recursion, YoKeith Thompson
|   || ||  | |  |  |`* Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | |  |  | `- Re: Recursion, YoKeith Thompson
|   || ||  | |  |  `* Re: Recursion, YoJanis Papanagnou
|   || ||  | |  |   `* Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | |  |    `* Re: Recursion, YoBen Bacarisse
|   || ||  | |  |     +* Re: Recursion, Yobart
|   || ||  | |  |     |`- Re: Recursion, YoBen Bacarisse
|   || ||  | |  |     `* Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | |  |      +* Re: Recursion, YoChris M. Thomasson
|   || ||  | |  |      |+* Re: Recursion, YoBen Bacarisse
|   || ||  | |  |      ||`* Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | |  |      || `* Re: Recursion, YoBen Bacarisse
|   || ||  | |  |      ||  `* Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | |  |      ||   `- Re: Recursion, YoJanis Papanagnou
|   || ||  | |  |      |`* Re: Recursion, YoJanis Papanagnou
|   || ||  | |  |      | +* Re: Recursion, YoLawrence D'Oliveiro
|   || ||  | |  |      | |`- Re: Recursion, YoJanis Papanagnou
|   || ||  | |  |      | `* Re: Recursion, YoMichael S
|   || ||  | |  |      |  +* Re: Recursion, YoTim Rentsch
|   || ||  | |  |      |  |+* Re: Recursion, YoScott Lurndal
|   || ||  | |  |      |  ||+- Re: Recursion, YoKeith Thompson
|   || ||  | |  |      |  ||`- Re: Recursion, YoTim Rentsch
|   || ||  | |  |      |  |+* Re: Recursion, Yobart
|   || ||  | |  |      |  ||`* Re: Recursion, YoBen Bacarisse
|   || ||  | |  |      |  || +- Re: Recursion, YoKeith Thompson
|   || ||  | |  |      |  || `- Re: Recursion, YoKaz Kylheku
|   || ||  | |  |      |  |`* Re: Recursion, YoKeith Thompson
|   || ||  | |  |      |  | `- Re: Recursion, YoTim Rentsch
|   || ||  | |  |      |  `- Re: Recursion, YoJanis Papanagnou
|   || ||  | |  |      `- Re: Recursion, YoBen Bacarisse
|   || ||  | |  +* Re: Recursion, Yobart
|   || ||  | |  |+* Re: Recursion, YoJanis Papanagnou
|   || ||  | |  ||`- Re: Recursion, Yobart
|   || ||  | |  |`- Re: Recursion, YoKeith Thompson
|   || ||  | |  `- Re: Recursion, YoTim Rentsch
|   || ||  | `- Re: Recursion, YoDavid Brown
|   || ||  `* Re: Recursion, YoKeith Thompson
|   || ||   `- Re: Recursion, YoDavid Brown
|   || |`- Re: Recursion, Yofir
|   || +- Re: Recursion, YoJanis Papanagnou
|   || +* Re: Recursion, YoScott Lurndal
|   || |`* Re: Recursion, YoLawrence D'Oliveiro
|   || | `* Re: Recursion, YoScott Lurndal
|   || |  `- Re: Recursion, YoBen Bacarisse
|   || +* Re: Recursion, YoKaz Kylheku
|   || |`- Re: Recursion, YoDavid Brown
|   || `* Re: Recursion, YoLawrence D'Oliveiro
|   |`- Re: Recursion, YoKaz Kylheku
|   `- Re: Recursion, YoTim Rentsch
`- Re: Recursion, YoLawrence D'Oliveiro

Pages:12345
Recursion, Yo

<uut24f$2icpb$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34959&group=comp.lang.c#34959

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Recursion, Yo
Date: Sun, 7 Apr 2024 02:58:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 105
Message-ID: <uut24f$2icpb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 07 Apr 2024 02:58:24 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="edc206e7c881f15813a37489a3e0be36";
logging-data="2700075"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RTf6uJbKcQkr7Hl/QA6P2"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:RtVscsf5Bt+NPOrmUqt93AN69wY=
 by: Lawrence D'Oliv - Sun, 7 Apr 2024 02:58 UTC

Some homies they be sayin’ recursion is Teh Evulz. Well, check this
out. This program be recursing to the max, yo.

Peace.

----
/*
Generate permutations of a list of items.
Pass a list of arbitary words as command arguments, and this
program will print out all possible orderings of them.
*/

#include <iso646.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

typedef void (*permute_action)
(
const char * const * words
);

void permute
(
unsigned int nrwords,
const char * const * words,
permute_action action
)
{
if (nrwords > 0)
{
const char ** const permbuf = (const char **)malloc(nrwords * sizeof(char *));
bool * const used = (bool *)malloc(nrwords);
for (unsigned int i = 0; i < nrwords; ++i)
{
used[i] = false;
} /*for*/

void permute1
(
unsigned int depth
)
{
if (depth < nrwords)
{
for (unsigned int i = 0; i < nrwords; ++i)
{
if (not used[i])
{
permbuf[depth] = words[i];
used[i] = true;
permute1(depth + 1);
used[i] = false;
} /*if*/
} /*for*/
}
else
{
action(permbuf);
} /*if*/
} /*permute1*/

permute1(0);

free(permbuf);
free(used);
} /*if*/
} /*permute*/

int main
(
int argc,
char ** argv
)
{
const unsigned int nrwords = argc - 1;
unsigned int count = 0;

void collect
(
const char * const * words
)
{
count += 1;
fprintf(stdout, "[%d](", count);
for (unsigned int i = 0; i < nrwords; ++i)
{
if (i != 0)
{
fputs(", ", stdout);
} /*if*/
fputs(words[i], stdout);
} /*for*/
fputs(")\n", stdout);
} /*collect*/

permute
(
/*nrwords =*/ nrwords,
/*words =*/ (const char * const * )(argv + 1),
/*permute_action =*/ (permute_action)collect
);

fprintf(stdout, "Nr found: %d\n", count);
} /*main*/

Re: Recursion, Yo

<uutqd2$bhl0$1@i2pn2.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34962&group=comp.lang.c#34962

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!.POSTED!not-for-mail
From: fir...@grunge.pl (fir)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Sun, 07 Apr 2024 11:52:29 +0200
Organization: i2pn2 (i2pn.org)
Message-ID: <uutqd2$bhl0$1@i2pn2.org>
References: <uut24f$2icpb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 7 Apr 2024 09:52:35 -0000 (UTC)
Injection-Info: i2pn2.org;
logging-data="378528"; mail-complaints-to="usenet@i2pn2.org";
posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
In-Reply-To: <uut24f$2icpb$1@dont-email.me>
X-Spam-Checker-Version: SpamAssassin 4.0.0
 by: fir - Sun, 7 Apr 2024 09:52 UTC

Lawrence D'Oliveiro wrote:
> Some homies they be sayin’ recursion is Teh Evulz. Well, check this
> out. This program be recursing to the max, yo.
>
> Peace.
>
> ----
> /*
> Generate permutations of a list of items.
> Pass a list of arbitary words as command arguments, and this
> program will print out all possible orderings of them.
> */
>
> #include <iso646.h>
> #include <stdbool.h>
> #include <stdlib.h>
> #include <stdio.h>
>
> typedef void (*permute_action)
> (
> const char * const * words
> );
>
> void permute
> (
> unsigned int nrwords,
> const char * const * words,
> permute_action action
> )
> {
> if (nrwords > 0)
> {
> const char ** const permbuf = (const char **)malloc(nrwords * sizeof(char *));
> bool * const used = (bool *)malloc(nrwords);
> for (unsigned int i = 0; i < nrwords; ++i)
> {
> used[i] = false;
> } /*for*/
>
> void permute1
> (
> unsigned int depth
> )
> {
> if (depth < nrwords)
> {
> for (unsigned int i = 0; i < nrwords; ++i)
> {
> if (not used[i])
> {
> permbuf[depth] = words[i];
> used[i] = true;
> permute1(depth + 1);
> used[i] = false;
> } /*if*/
> } /*for*/
> }
> else
> {
> action(permbuf);
> } /*if*/
> } /*permute1*/
>
> permute1(0);
>
> free(permbuf);
> free(used);
> } /*if*/
> } /*permute*/
>
> int main
> (
> int argc,
> char ** argv
> )
> {
> const unsigned int nrwords = argc - 1;
> unsigned int count = 0;
>
> void collect
> (
> const char * const * words
> )
> {
> count += 1;
> fprintf(stdout, "[%d](", count);
> for (unsigned int i = 0; i < nrwords; ++i)
> {
> if (i != 0)
> {
> fputs(", ", stdout);
> } /*if*/
> fputs(words[i], stdout);
> } /*for*/
> fputs(")\n", stdout);
> } /*collect*/
>
> permute
> (
> /*nrwords =*/ nrwords,
> /*words =*/ (const char * const * )(argv + 1),
> /*permute_action =*/ (permute_action)collect
> );
>
> fprintf(stdout, "Nr found: %d\n", count);
> } /*main*/
>
okay, there are some class of things that suit good for recursion - and
its probably good to spot whose they are

Re: Recursion, Yo

<uv2u2a$41j5$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34986&group=comp.lang.c#34986

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 9 Apr 2024 10:25:46 +0200
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <uv2u2a$41j5$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 09 Apr 2024 08:25:47 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e5402271be07e4c30967281c98e603b9";
logging-data="132709"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19QOFKahbPeMV5nMki7N8Kt"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:KD20qhSJu0RiCYJxjNPDWXB7iPA=
In-Reply-To: <uutqd2$bhl0$1@i2pn2.org>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Tue, 9 Apr 2024 08:25 UTC

On 07.04.2024 11:52, fir wrote:
>>
> okay, there are some class of things that suit good for recursion - and
> its probably good to spot whose they are

All iterations. (So, basically all code we typically write with 'for'
and 'while' loops.) For example. Plus all the things where emulation
with non-recursive means is cumbersome and complex. (Like operating
on recursive data structures like trees.)

Actually, you can formally derive the iterative constructs from [a
subset of] the recursive ones (by introducing variables and loops).

It makes a difference where one comes from. If folks learned coding
from imperative languages they have more problems with a functional
view (and also with recursion, it seems). Here, with "C", we're more
used to 'while' loops and variables, I think.

Janis

Re: Recursion, Yo

<uv2v2o$42fo$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34987&group=comp.lang.c#34987

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 9 Apr 2024 08:43:05 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <uv2v2o$42fo$3@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 09 Apr 2024 08:43:05 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="c62da5eaa27d15bf3b04834dfd3595c9";
logging-data="133624"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18T4tjUda2b6UyqBW9x/eQq"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:3prVIiH/Btzi7It5EGeIy5VK11c=
 by: Lawrence D'Oliv - Tue, 9 Apr 2024 08:43 UTC

On Tue, 9 Apr 2024 10:25:46 +0200, Janis Papanagnou wrote:

> If folks learned coding from imperative languages they have more
> problems with a functional view (and also with recursion, it seems).

One of the first few books I ever read on Comp Sci (while still at school,
back when schools didn’t have computers) was “A Comparative Study Of
Programming Languages” by Bryan Higman. In one of the chapters, I came
across the concept of recursion. I also came across Ackermann’s Function.

Let’s just say that, after staring into that abyss, nothing about
recursion could possibly scare me, ever again ...

Re: Recursion, Yo

<uv2v4u$42fo$4@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34988&group=comp.lang.c#34988

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 9 Apr 2024 08:44:14 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <uv2v4u$42fo$4@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 09 Apr 2024 08:44:14 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="c62da5eaa27d15bf3b04834dfd3595c9";
logging-data="133624"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/36it/IAVxq3CCVLOvEy2F"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:kTmQpCCebnxXIPJLBzJ5psZqtFE=
 by: Lawrence D'Oliv - Tue, 9 Apr 2024 08:44 UTC

> /*
> Generate permutations of a list of items.
> Pass a list of arbitary words as command arguments, and this program
> will print out all possible orderings of them.
> */

Example run:

./permute a b c

Output:

[1](a, b, c)
[2](a, c, b)
[3](b, a, c)
[4](b, c, a)
[5](c, a, b)
[6](c, b, a)
Nr found: 6

Re: Recursion, Yo

<87edbestmg.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34991&group=comp.lang.c#34991

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 09 Apr 2024 11:44:23 +0100
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <87edbestmg.fsf@bsb.me.uk>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Tue, 09 Apr 2024 10:44:23 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="fa324957339a8e3c51ed3c4958d6166a";
logging-data="190708"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/p2l6WReLicjOkbZrokwxtpSVj3VbCXBU="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:insFUBr5BfFnXzJ5BHrIiHs+R/w=
sha1:Yyr/QlNmYaL/6pBbB44AKYBHTCQ=
X-BSB-Auth: 1.e52c8e6f61ffb5397744.20240409114423BST.87edbestmg.fsf@bsb.me.uk
 by: Ben Bacarisse - Tue, 9 Apr 2024 10:44 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:

> On 07.04.2024 11:52, fir wrote:
>>>
>> okay, there are some class of things that suit good for recursion - and
>> its probably good to spot whose they are
>
> All iterations. (So, basically all code we typically write with 'for'
> and 'while' loops.) For example. Plus all the things where emulation
> with non-recursive means is cumbersome and complex. (Like operating
> on recursive data structures like trees.)
>
> Actually, you can formally derive the iterative constructs from [a
> subset of] the recursive ones (by introducing variables and loops).
>
> It makes a difference where one comes from. If folks learned coding
> from imperative languages they have more problems with a functional
> view (and also with recursion, it seems). Here, with "C", we're more
> used to 'while' loops and variables, I think.

The language used is key. I would not say that recursion is a good fit
for "all iterations" when using C. It's significant (or at least note
worthy) that the code in the original post was not ISO C and re-writing
it in ISO C would show up some of the issues involved, though this task
is still a good fit for an explicitly recursive solution.

--
Ben.

Re: Recursion, Yo

<877ch6soxc.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34992&group=comp.lang.c#34992

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 09 Apr 2024 13:25:51 +0100
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <877ch6soxc.fsf@bsb.me.uk>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Tue, 09 Apr 2024 12:25:53 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="fa324957339a8e3c51ed3c4958d6166a";
logging-data="243622"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+W3NUekOYg8+3wjuiVd/ySuRplT1kHEts="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:BhhsCZLEjwDr89Ibk3BAbs9OpJU=
sha1:ePNU8NQfEFYj4Rq8bbIdqB2rvPk=
X-BSB-Auth: 1.bb81fd415cb00a01fa60.20240409132551BST.877ch6soxc.fsf@bsb.me.uk
 by: Ben Bacarisse - Tue, 9 Apr 2024 12:25 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>
>> On 07.04.2024 11:52, fir wrote:
>>>>
>>> okay, there are some class of things that suit good for recursion - and
>>> its probably good to spot whose they are
>>
>> All iterations. (So, basically all code we typically write with 'for'
>> and 'while' loops.) For example. Plus all the things where emulation
>> with non-recursive means is cumbersome and complex. (Like operating
>> on recursive data structures like trees.)
>>
>> Actually, you can formally derive the iterative constructs from [a
>> subset of] the recursive ones (by introducing variables and loops).
>>
>> It makes a difference where one comes from. If folks learned coding
>> from imperative languages they have more problems with a functional
>> view (and also with recursion, it seems). Here, with "C", we're more
>> used to 'while' loops and variables, I think.
>
> The language used is key. I would not say that recursion is a good fit
> for "all iterations" when using C. It's significant (or at least note
> worthy) that the code in the original post was not ISO C and re-writing
> it in ISO C would show up some of the issues involved, though this task
> is still a good fit for an explicitly recursive solution.

Though I want to say that for many application in standard C the use of
a "visitor function" makes the OP's code awkward. Instead, I would
probably iterate over the permutations like this:

#include <stdbool.h>
#include <stdio.h>

void swap(int *a, int *b)
{ int t = *a; *a = *b; *b = t;
}

bool next_perm(int n, int *p)
{ int i = n - 2;
while (i >= 0 && p[i] >= p[i+1]) i--;
// i is now -1 or the largest index with p[i] < p[i+1]
if (i < 0)
return false;
int j = n - 1;
while (p[j] <= p[i]) j--;
// j is now the largest index > i with p[j] > p[i]
swap(&p[i], &p[j]);
// reverse p[i+1] ... p[n-1]
int l = i, h = n;
while (++l < --h)
swap(&p[l], &p[h]);
return true;
}

int main(int argc, char **argv)
{ if (argc > 1) {
int n = argc - 1, idx[n];
for (int i = 0; i < n; i++)
idx[i] = i;
do for (int i = 0; i < n; i++)
printf("%s%s", argv[idx[i] + 1], i == n-1 ? "\n" : " ");
while (next_perm(n, idx));
}
}

--
Ben.

Re: Recursion, Yo

<uv3c77$7eb5$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34993&group=comp.lang.c#34993

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 9 Apr 2024 14:27:19 +0200
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <uv3c77$7eb5$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <uv2v2o$42fo$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 09 Apr 2024 12:27:20 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e5402271be07e4c30967281c98e603b9";
logging-data="244069"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IS55XBQJs1T+DBzu2eRzW"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:hHhqU6clBgS6h5Pjsa4gsM4IYQE=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <uv2v2o$42fo$3@dont-email.me>
 by: Janis Papanagnou - Tue, 9 Apr 2024 12:27 UTC

On 09.04.2024 10:43, Lawrence D'Oliveiro wrote:
>
> One of the first few books I ever read on Comp Sci (while still at school,
> back when schools didn’t have computers) was “A Comparative Study Of
> Programming Languages” by Bryan Higman. In one of the chapters, I came
> across the concept of recursion. I also came across Ackermann’s Function.
>
> Let’s just say that, after staring into that abyss, nothing about
> recursion could possibly scare me, ever again ...

Well, Ackermann is a good example how to scare folks - especially
if folks implement it straightforward recursively, then running it
and waiting for termination. :-)

But there's also less complex algorithms, like Fibonacci, that have
a bad runtime complexity if implemented in a trivial way. Though that
depends on how you actually implement it[*] and it's not an inherent
[bad] property of recursion (as sometimes wrongly assumed).

Janis

[*] I once wrote (for an "obfuscated Awk" post) the usual definition
(with some nasty side-effects, granted), which basically was

func __(_){return _>2?__(--_)+__(--_):1}

The (for this thread) noteworthy detail is that you can transform the
function to

func ___(_) { return __(_,x^x,x^x^x) }
func __(_,_x,x_) { return --_?__(_,x_,_x+x_):_x+x_ }

which is also very fast for larger arguments, while the implementation
still being within the (non-imperative) recursive functions paradigm.

PS: I hope *this* scared you at least a bit, since that was one of the
original purposes of the post. ;-)

PPS: Rewrite to "C" left as an exercise to the reader.

Re: Recursion, Yo

<uv3hpq$8m48$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34994&group=comp.lang.c#34994

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 9 Apr 2024 15:02:35 +0100
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <uv3hpq$8m48$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <uv2v2o$42fo$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 09 Apr 2024 14:02:35 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="d512bc1b765678643e8f7017c196808b";
logging-data="284808"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+AAc08NTXc1AD794AjMQM6"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:J5kVaCuX93/TqP4TGaGucTFurcA=
Content-Language: en-GB
In-Reply-To: <uv2v2o$42fo$3@dont-email.me>
 by: bart - Tue, 9 Apr 2024 14:02 UTC

On 09/04/2024 09:43, Lawrence D'Oliveiro wrote:
> On Tue, 9 Apr 2024 10:25:46 +0200, Janis Papanagnou wrote:
>
>> If folks learned coding from imperative languages they have more
>> problems with a functional view (and also with recursion, it seems).
>
> One of the first few books I ever read on Comp Sci (while still at school,
> back when schools didn’t have computers) was “A Comparative Study Of
> Programming Languages” by Bryan Higman. In one of the chapters, I came
> across the concept of recursion. I also came across Ackermann’s Function.
>
> Let’s just say that, after staring into that abyss, nothing about
> recursion could possibly scare me, ever again ...

This one is a bit scarier:

https://en.wikipedia.org/wiki/Man_or_boy_test

Although normally requiring closures, there are many implementations
here that work around that, including for C:

https://rosettacode.org/wiki/Man_or_boy_test

There is also a C version that relies on gcc's (gnuC) extensions for
nested function, but when I tested it, it was nearly 200 times slower
than plain C.

Re: Recursion, Yo

<871q7esjbc.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34995&group=comp.lang.c#34995

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 09 Apr 2024 15:27:03 +0100
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <871q7esjbc.fsf@bsb.me.uk>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <uv2v2o$42fo$3@dont-email.me>
<uv3c77$7eb5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 09 Apr 2024 14:27:05 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="fa324957339a8e3c51ed3c4958d6166a";
logging-data="299724"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19eZEcpBRPRNd5wVxcze2820G/E4Kk/pho="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:UoGOQ0tgI50CZ6g3CeDc5oSMkiU=
sha1:/zArULlq19B7/FzZF8T+XsgWykY=
X-BSB-Auth: 1.3120ae427139f28b88b3.20240409152703BST.871q7esjbc.fsf@bsb.me.uk
 by: Ben Bacarisse - Tue, 9 Apr 2024 14:27 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:

> On 09.04.2024 10:43, Lawrence D'Oliveiro wrote:
>>
>> One of the first few books I ever read on Comp Sci (while still at school,
>> back when schools didn’t have computers) was “A Comparative Study Of
>> Programming Languages” by Bryan Higman. In one of the chapters, I came
>> across the concept of recursion. I also came across Ackermann’s Function.
>>
>> Let’s just say that, after staring into that abyss, nothing about
>> recursion could possibly scare me, ever again ...
>
> Well, Ackermann is a good example how to scare folks - especially
> if folks implement it straightforward recursively, then running it
> and waiting for termination. :-)

Ackemann's function in FORTRAN (which had no recursion in those days)
used to be a student programming assignment. Not so much scary as a
good learning exercise.

> But there's also less complex algorithms, like Fibonacci, that have
> a bad runtime complexity if implemented in a trivial way. Though that
> depends on how you actually implement it[*] and it's not an inherent
> [bad] property of recursion (as sometimes wrongly assumed).

Yes. Haskell's

fib = 1 : 1 : zipWith (+) fib (tail fib)

is (in Haskell terms) quite efficient.

> [*] I once wrote (for an "obfuscated Awk" post) the usual definition
> (with some nasty side-effects, granted), which basically was
>
> func __(_){return _>2?__(--_)+__(--_):1}
>
> The (for this thread) noteworthy detail is that you can transform the
> function to
>
> func ___(_) { return __(_,x^x,x^x^x) }
> func __(_,_x,x_) { return --_?__(_,x_,_x+x_):_x+x_ }

The trouble with AWK (without gawk's -M) is that functions like this
just start to give a wrong, but plausibly sized, result. For example
___(80) is 23416728348467684 when it should be 23416728348467685.

--
Ben.

Re: Recursion, Yo

<uv46eu$e0ps$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34998&group=comp.lang.c#34998

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 9 Apr 2024 21:55:08 +0200
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <uv46eu$e0ps$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <uv2v2o$42fo$3@dont-email.me>
<uv3c77$7eb5$1@dont-email.me> <871q7esjbc.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 09 Apr 2024 19:55:10 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e5402271be07e4c30967281c98e603b9";
logging-data="459580"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19lQuyMlTfT7oeNh6OrCNkR"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:UNz7eWfb+xePKB7aVIvQBd6q3jU=
In-Reply-To: <871q7esjbc.fsf@bsb.me.uk>
 by: Janis Papanagnou - Tue, 9 Apr 2024 19:55 UTC

On 09.04.2024 16:27, Ben Bacarisse wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>
>> func ___(_) { return __(_,x^x,x^x^x) }
>> func __(_,_x,x_) { return --_?__(_,x_,_x+x_):_x+x_ }
>
> The trouble with AWK (without gawk's -M) is that functions like this
> just start to give a wrong, but plausibly sized, result. For example
> ___(80) is 23416728348467684 when it should be 23416728348467685.

A general issue with functions and arithmetic producing "too large"
numbers with languages and systems that don't support them. Yes.

I'd prefer at least an error indication, though, and not a silent
overflow.

Janis

Re: Recursion, Yo

<87o7aiffbv.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35001&group=comp.lang.c#35001

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 09 Apr 2024 13:31:32 -0700
Organization: None to speak of
Lines: 30
Message-ID: <87o7aiffbv.fsf@nosuchdomain.example.com>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <uv2v2o$42fo$3@dont-email.me>
<uv3c77$7eb5$1@dont-email.me> <871q7esjbc.fsf@bsb.me.uk>
<uv46eu$e0ps$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Tue, 09 Apr 2024 20:31:32 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="41710cd94b54dc6d2c462f463b62aa07";
logging-data="474487"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/sRARer5+N8bqCOK71PrRj"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:jPNbKdAQsX1qfjOzPuOs1ZAfq3U=
sha1:TNl6Xmq+TkFNXn/SnpLwa1mpXnA=
 by: Keith Thompson - Tue, 9 Apr 2024 20:31 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 09.04.2024 16:27, Ben Bacarisse wrote:
>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>>
>>> func ___(_) { return __(_,x^x,x^x^x) }
>>> func __(_,_x,x_) { return --_?__(_,x_,_x+x_):_x+x_ }
>>
>> The trouble with AWK (without gawk's -M) is that functions like this
>> just start to give a wrong, but plausibly sized, result. For example
>> ___(80) is 23416728348467684 when it should be 23416728348467685.
>
> A general issue with functions and arithmetic producing "too large"
> numbers with languages and systems that don't support them. Yes.
>
> I'd prefer at least an error indication, though, and not a silent
> overflow.

If I understand correctly, it's not overflow, it's quiet loss of
precision.

awk doesn't normally distinguish between integer and floating-point
numbers, and uses FP to represent integers. A computation that should
yield 23416728348467685 doesn't overflow, but it quietly yields
23416728348467684. (`gawk -M` supports arbitrary precision integer
arithmetic.)

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Recursion, Yo

<uv4r9e$mdd3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35002&group=comp.lang.c#35002

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 01:50:38 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 141
Message-ID: <uv4r9e$mdd3$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Apr 2024 01:50:39 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7ce03bb5457ab82bf7e463e8fecedba8";
logging-data="734627"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OtVl/NF0OWkRX9OBkkXIR"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:6U9Yvkpu1qOMQhYxKD0reuSyqyc=
 by: Lawrence D'Oliv - Wed, 10 Apr 2024 01:50 UTC

On Tue, 09 Apr 2024 11:44:23 +0100, Ben Bacarisse wrote:

> It's significant (or at least note worthy) that the code in the
> original post was not ISO C ...

Interesting that GCC’s C compiler allows nested routine definitions,
but the C++ compiler does not.

> and re-writing it in ISO C would show up some of the issues involved
> ...

Ask and you shall receive ...

/*
Generate permutations of a list of items.
Pass a list of arbitary words as command arguments, and this
program will print out all possible orderings of them.
*/

#include <iso646.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

typedef void (*permute_action)
(
unsigned int nrwords,
const char * const * words,
void * arg
);

struct permute_ctx
{
unsigned int nrwords;
const char * const * words;
permute_action action;
void * action_arg;
const char ** permbuf;
bool * used;
} /*permute_ctx*/;

static void permute1
(
struct permute_ctx * ctx,
unsigned int depth
)
{
if (depth < ctx->nrwords)
{
for (unsigned int i = 0; i < ctx->nrwords; ++i)
{
if (not ctx->used[i])
{
ctx->permbuf[depth] = ctx->words[i];
ctx->used[i] = true;
permute1(ctx, depth + 1);
ctx->used[i] = false;
} /*if*/
} /*for*/
}
else
{
ctx->action(ctx->nrwords, ctx->permbuf, ctx->action_arg);
} /*if*/
} /*permute1*/

void permute
(
unsigned int nrwords,
const char * const * words,
permute_action action,
void * action_arg
)
{
if (nrwords > 0)
{
struct permute_ctx ctx;
ctx.nrwords = nrwords;
ctx.words = words;
ctx.action = action;
ctx.action_arg = action_arg;
ctx.permbuf = (const char **)malloc(nrwords * sizeof(char *));
ctx.used = (bool *)malloc(nrwords);
for (unsigned int i = 0; i < nrwords; ++i)
{
ctx.used[i] = false;
} /*for*/

permute1(&ctx, 0);

free(ctx.permbuf);
free(ctx.used);
} /*if*/
} /*permute*/

struct main_ctx
{
FILE * out;
unsigned int count;
} /*main_ctx*/;

void collect
(
unsigned int nrwords,
const char * const * words,
struct main_ctx * ctx
)
{
ctx->count += 1;
fprintf(ctx->out, "[%d](", ctx->count);
for (unsigned int i = 0; i < nrwords; ++i)
{
if (i != 0)
{
fputs(", ", ctx->out);
} /*if*/
fputs(words[i], ctx->out);
} /*for*/
fputs(")\n", ctx->out);
} /*collect*/

int main
(
int argc,
char ** argv
)
{
struct main_ctx ctx;
ctx.out = stdout;
ctx.count = 0;

permute
(
/*nrwords =*/ argc - 1,
/*words =*/ (const char * const * )(argv + 1),
/*permute_action =*/ (permute_action)collect,
/*action_arg =*/ (void *)&ctx
);

fprintf(stdout, "Nr found: %d\n", ctx.count);
} /*main*/

Re: Recursion, Yo

<uv52h9$o0s2$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35003&group=comp.lang.c#35003

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Tue, 9 Apr 2024 20:54:17 -0700
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <uv52h9$o0s2$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
<uv4r9e$mdd3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Apr 2024 03:54:18 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="1e0154287d270c974cd6798ddf950547";
logging-data="787330"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+nNE6eDF6KTf887SYZAUsBQgUThiRxHwo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:qPhVIyvcy+U+RCZ9cl4y9lq6uu8=
Content-Language: en-US
In-Reply-To: <uv4r9e$mdd3$1@dont-email.me>
 by: Chris M. Thomasson - Wed, 10 Apr 2024 03:54 UTC

On 4/9/2024 6:50 PM, Lawrence D'Oliveiro wrote:
> On Tue, 09 Apr 2024 11:44:23 +0100, Ben Bacarisse wrote:
>
>> It's significant (or at least note worthy) that the code in the
>> original post was not ISO C ...
>
> Interesting that GCC’s C compiler allows nested routine definitions,
> but the C++ compiler does not.
>
>> and re-writing it in ISO C would show up some of the issues involved
>> ...
>
> Ask and you shall receive ...
[...]

Are you an AI?

Re: Recursion, Yo

<uv5blv$psbu$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35004&group=comp.lang.c#35004

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 08:30:23 +0200
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <uv5blv$psbu$2@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <uv2v2o$42fo$3@dont-email.me>
<uv3c77$7eb5$1@dont-email.me> <871q7esjbc.fsf@bsb.me.uk>
<uv46eu$e0ps$1@dont-email.me> <87o7aiffbv.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 10 Apr 2024 06:30:23 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="63977abd1b4cb9268d9f2058a222d662";
logging-data="848254"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//aX3ehcN8Ehiw+VUZ/mnv"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:XzmKorjdv1JHtSYmV+EtRRQ2y/w=
In-Reply-To: <87o7aiffbv.fsf@nosuchdomain.example.com>
 by: Janis Papanagnou - Wed, 10 Apr 2024 06:30 UTC

On 09.04.2024 22:31, Keith Thompson wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>> On 09.04.2024 16:27, Ben Bacarisse wrote:
>>
>> A general issue with functions and arithmetic producing "too large"
>> numbers with languages and systems that don't support them. Yes.
>>
>> I'd prefer at least an error indication, though, and not a silent
>> overflow.
>
> If I understand correctly, it's not overflow, it's quiet loss of
> precision.

Yes, you are right.

Janis

Re: Recursion, Yo

<uv5e3l$q885$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35006&group=comp.lang.c#35006

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 09:11:49 +0200
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <uv5e3l$q885$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
<uv4r9e$mdd3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Apr 2024 07:11:50 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7a89ab18d1e4b124320c42cc3e6d2a2d";
logging-data="860421"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+b6AmkLiEt1qMLoewsSRVoHhUYbkXWUzE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:7cfAs1Yj+A7hFVg1YucpE3xV0UA=
Content-Language: en-GB
In-Reply-To: <uv4r9e$mdd3$1@dont-email.me>
 by: David Brown - Wed, 10 Apr 2024 07:11 UTC

On 10/04/2024 03:50, Lawrence D'Oliveiro wrote:
> On Tue, 09 Apr 2024 11:44:23 +0100, Ben Bacarisse wrote:
>
>> It's significant (or at least note worthy) that the code in the
>> original post was not ISO C ...
>
> Interesting that GCC’s C compiler allows nested routine definitions,
> but the C++ compiler does not.

It is an old extension, going back to when gcc barely (if at all)
supported C++. The compiler middle-end had to have support for nested
functions for languages like Pascal, Module 2 and Ada (I believe Ada is
the only one of these that stuck around in gcc mainline, but other
language front-ends have been made outside the main tree). Someone
thought it might be a useful feature in C too, and perhaps something
that would catch on in the standards (several early gcc extensions ended
up standardised in C99).

It is not much used in practice, AFAIK. For some cases the code
generation for nested functions was fine and straight-forward. In other
cases, however, it required a trampoline generated on the stack, and
that became a real pain once non-executable stacks came into fashion.

Nested functions were never as interesting for C++ as you already have
better mechanisms for controlling scope and data access - classes and
their methods, including nested classes. And once lambdas joined the
party in C++11 there was absolutely no reason to have nested functions -
there is nothing (AFAIK) that you could do with nested functions that
you can't do at least as well, and often better, with lambdas.

Re: Recursion, Yo

<uv5gfd$qum1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35007&group=comp.lang.c#35007

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 07:52:13 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <uv5gfd$qum1$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
<uv4r9e$mdd3$1@dont-email.me> <uv5e3l$q885$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Apr 2024 07:52:13 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7ce03bb5457ab82bf7e463e8fecedba8";
logging-data="883393"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/vXy2eLM0xd/RFAmfCqcbs"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:oMnUf76G54o6qrBCTFSOF+yE9yE=
 by: Lawrence D'Oliv - Wed, 10 Apr 2024 07:52 UTC

On Wed, 10 Apr 2024 09:11:49 +0200, David Brown wrote:

> It is not much used in practice, AFAIK. For some cases the code
> generation for nested functions was fine and straight-forward. In other
> cases, however, it required a trampoline generated on the stack, and
> that became a real pain once non-executable stacks came into fashion.

That would be true of those other languages that require the feature, too.

> Nested functions were never as interesting for C++ as you already have
> better mechanisms for controlling scope and data access - classes and
> their methods, including nested classes.

Python does both. Just because you have classes doesn’t mean functions
can’t be first-class objects, too.

Re: Recursion, Yo

<uv5lgl$s6uj$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35009&group=comp.lang.c#35009

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 11:18:12 +0200
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <uv5lgl$s6uj$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
<uv4r9e$mdd3$1@dont-email.me> <uv5e3l$q885$1@dont-email.me>
<uv5gfd$qum1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Apr 2024 09:18:13 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7a89ab18d1e4b124320c42cc3e6d2a2d";
logging-data="924627"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ooe+TJOufLewbNwBdJQf+0Mpth/TN0Gg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:sFmgHvS+LFrtYt92+5ZFIhcJvIg=
Content-Language: en-GB
In-Reply-To: <uv5gfd$qum1$1@dont-email.me>
 by: David Brown - Wed, 10 Apr 2024 09:18 UTC

On 10/04/2024 09:52, Lawrence D'Oliveiro wrote:
> On Wed, 10 Apr 2024 09:11:49 +0200, David Brown wrote:
>
>> It is not much used in practice, AFAIK. For some cases the code
>> generation for nested functions was fine and straight-forward. In other
>> cases, however, it required a trampoline generated on the stack, and
>> that became a real pain once non-executable stacks came into fashion.
>
> That would be true of those other languages that require the feature, too.
>

There may be other differences in the languages that reduce that effect
- or it could be a problem there too. I don't know the details.
(Perhaps those on an Ada newsgroup would know better.)

The problem with trampolines comes about when you want to take the
address of the nested function and use that outside of the surrounding
function, while you also have variables captured by reference.

>> Nested functions were never as interesting for C++ as you already have
>> better mechanisms for controlling scope and data access - classes and
>> their methods, including nested classes.
>
> Python does both. Just because you have classes doesn’t mean functions
> can’t be first-class objects, too.

True. But Python is a very different language from C++. In Python, not
only are functions objects in themselves, but so are classes (the
definition of the classes, rather than just instances). Python is a lot
more "meta" than C++.

Basically, anything you could do with a nested function in gcc C you can
do in C++:

int sum_square(int x, int y) {
int square(z) {
return z * z;
}
return square(x) + square(y);
}

int sum_square(int x, int y) {
auto square = [](int z) {
return z * z;
}
return square(x) + square(y);
}

Re: Recursion, Yo

<uv61f6$v1jm$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35010&group=comp.lang.c#35010

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 13:42:14 +0100
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <uv61f6$v1jm$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
<uv4r9e$mdd3$1@dont-email.me> <uv5e3l$q885$1@dont-email.me>
<uv5gfd$qum1$1@dont-email.me> <uv5lgl$s6uj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Apr 2024 12:42:14 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="509425d4a7e19d9832405743df5292cc";
logging-data="1017462"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/xaKY97xOrSbPGxWHegRA"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:f7LWSx5QbMAIOYqJW/fYs6v1i4c=
Content-Language: en-GB
In-Reply-To: <uv5lgl$s6uj$1@dont-email.me>
 by: bart - Wed, 10 Apr 2024 12:42 UTC

On 10/04/2024 10:18, David Brown wrote:
> On 10/04/2024 09:52, Lawrence D'Oliveiro wrote:
>> On Wed, 10 Apr 2024 09:11:49 +0200, David Brown wrote:
>>
>>> It is not much used in practice, AFAIK.  For some cases the code
>>> generation for nested functions was fine and straight-forward.  In other
>>> cases, however, it required a trampoline generated on the stack, and
>>> that became a real pain once non-executable stacks came into fashion.
>>
>> That would be true of those other languages that require the feature,
>> too.
>>
>
> There may be other differences in the languages that reduce that effect
> - or it could be a problem there too.  I don't know the details.
> (Perhaps those on an Ada newsgroup would know better.)
>
> The problem with trampolines comes about when you want to take the
> address of the nested function and use that outside of the surrounding
> function, while you also have variables captured by reference.
>
>>> Nested functions were never as interesting for C++ as you already have
>>> better mechanisms for controlling scope and data access - classes and
>>> their methods, including nested classes.
>>
>> Python does both. Just because you have classes doesn’t mean functions
>> can’t be first-class objects, too.
>
> True.  But Python is a very different language from C++.  In Python, not
> only are functions objects in themselves, but so are classes (the
> definition of the classes, rather than just instances).  Python is a lot
> more "meta" than C++.
>
> Basically, anything you could do with a nested function in gcc C you can
> do in C++:
>
> int sum_square(int x, int y) {
>     int square(z) { // int z ??
>         return z * z;
>     }
>     return square(x) + square(y);
> }

That's not an interesting use of a local function! You can move square()
outside, and it would still work, putting aside any clashes with
existing names called 'square'.

The challenges of local functions are to do with accessing transient
variables belonging to their enclosing function. Especially when a
reference to the local function is called from outside the lexical scope
of the enclosing function, with extra difficulties when the enclosing
function is no longer active.

> int sum_square(int x, int y) {
>     auto square = [](int z) {
>         return z * z;
>     }
>     return square(x) + square(y);
> }

What's the significance of the '[]' here?

Re: Recursion, Yo

<uv63ir$vn86$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35011&group=comp.lang.c#35011

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 15:18:18 +0200
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <uv63ir$vn86$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
<uv4r9e$mdd3$1@dont-email.me> <uv5e3l$q885$1@dont-email.me>
<uv5gfd$qum1$1@dont-email.me> <uv5lgl$s6uj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Apr 2024 13:18:20 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="63977abd1b4cb9268d9f2058a222d662";
logging-data="1039622"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19O4W4bZkFcUr6QGVw7wSM7"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:ICFLsCprR27gM1cKe+lQusl9wEs=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <uv5lgl$s6uj$1@dont-email.me>
 by: Janis Papanagnou - Wed, 10 Apr 2024 13:18 UTC

On 10.04.2024 11:18, David Brown wrote:
> On 10/04/2024 09:52, Lawrence D'Oliveiro wrote:
>> On Wed, 10 Apr 2024 09:11:49 +0200, David Brown wrote:
>
> The problem with trampolines comes about when you want to take the
> address of the nested function and use that outside of the surrounding
> function, while you also have variables captured by reference.

Sounds like a C-issue (not one of nesting functions). Usually I use
nested functions to have and use them locally and not in surrounding
or global scope. - I'd expect a C compiler to notice such situations.

>
>>> Nested functions were never as interesting for C++ as you already have
>>> better mechanisms for controlling scope and data access - classes and
>>> their methods, including nested classes.
>>
>> Python does both. Just because you have classes doesn’t mean functions
>> can’t be first-class objects, too.

Indeed.

(You could also omit use of C++ templates completely and emulate them
with class inheritance; nonetheless they are useful and convenient to
have them supported as concept as well. - Just for another example.)

Janis

Re: Recursion, Yo

<KhxRN.169893$t8cc.156830@fx06.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35013&group=comp.lang.c#35013

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!nntp.comgw.net!peer02.ams4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx06.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Recursion, Yo
Newsgroups: comp.lang.c
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org> <uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk> <uv4r9e$mdd3$1@dont-email.me> <uv5e3l$q885$1@dont-email.me> <uv5gfd$qum1$1@dont-email.me> <uv5lgl$s6uj$1@dont-email.me>
Lines: 25
Message-ID: <KhxRN.169893$t8cc.156830@fx06.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 10 Apr 2024 14:23:38 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 10 Apr 2024 14:23:38 GMT
X-Received-Bytes: 1971
 by: Scott Lurndal - Wed, 10 Apr 2024 14:23 UTC

David Brown <david.brown@hesbynett.no> writes:
>On 10/04/2024 09:52, Lawrence D'Oliveiro wrote:
>> On Wed, 10 Apr 2024 09:11:49 +0200, David Brown wrote:
>>
>>> It is not much used in practice, AFAIK. For some cases the code
>>> generation for nested functions was fine and straight-forward. In other
>>> cases, however, it required a trampoline generated on the stack, and
>>> that became a real pain once non-executable stacks came into fashion.
>>
>> That would be true of those other languages that require the feature, too.
>>
>
>There may be other differences in the languages that reduce that effect
>- or it could be a problem there too. I don't know the details.
>(Perhaps those on an Ada newsgroup would know better.)
>
>The problem with trampolines comes about when you want to take the
>address of the nested function and use that outside of the surrounding
>function, while you also have variables captured by reference.

The Burroughs Algol systems used something called lex levels to
handle nested functions.

https://en.wikipedia.org/wiki/Burroughs_Large_Systems#Stack_architecture

Re: Recursion, Yo

<uv68ok$11080$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35015&group=comp.lang.c#35015

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 16:46:43 +0200
Organization: A noiseless patient Spider
Lines: 109
Message-ID: <uv68ok$11080$1@dont-email.me>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
<uv4r9e$mdd3$1@dont-email.me> <uv5e3l$q885$1@dont-email.me>
<uv5gfd$qum1$1@dont-email.me> <uv5lgl$s6uj$1@dont-email.me>
<uv61f6$v1jm$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Apr 2024 14:46:45 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7a89ab18d1e4b124320c42cc3e6d2a2d";
logging-data="1081600"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18EpPUHPh4JVI4Q/kJSUqV9Es9SmUTR3yQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:6u7x5THfQt9gAuQMwWgRn0U19Pw=
Content-Language: en-GB
In-Reply-To: <uv61f6$v1jm$1@dont-email.me>
 by: David Brown - Wed, 10 Apr 2024 14:46 UTC

On 10/04/2024 14:42, bart wrote:
> On 10/04/2024 10:18, David Brown wrote:
>> On 10/04/2024 09:52, Lawrence D'Oliveiro wrote:
>>> On Wed, 10 Apr 2024 09:11:49 +0200, David Brown wrote:
>>>
>>>> It is not much used in practice, AFAIK.  For some cases the code
>>>> generation for nested functions was fine and straight-forward.  In
>>>> other
>>>> cases, however, it required a trampoline generated on the stack, and
>>>> that became a real pain once non-executable stacks came into fashion.
>>>
>>> That would be true of those other languages that require the feature,
>>> too.
>>>
>>
>> There may be other differences in the languages that reduce that
>> effect - or it could be a problem there too.  I don't know the
>> details. (Perhaps those on an Ada newsgroup would know better.)
>>
>> The problem with trampolines comes about when you want to take the
>> address of the nested function and use that outside of the surrounding
>> function, while you also have variables captured by reference.
>>
>>>> Nested functions were never as interesting for C++ as you already have
>>>> better mechanisms for controlling scope and data access - classes and
>>>> their methods, including nested classes.
>>>
>>> Python does both. Just because you have classes doesn’t mean functions
>>> can’t be first-class objects, too.
>>
>> True.  But Python is a very different language from C++.  In Python,
>> not only are functions objects in themselves, but so are classes (the
>> definition of the classes, rather than just instances).  Python is a
>> lot more "meta" than C++.
>>
>> Basically, anything you could do with a nested function in gcc C you
>> can do in C++:
>>
>> int sum_square(int x, int y) {
>>      int square(z) {               // int z ??
>>          return z * z;
>>      }
>>      return square(x) + square(y);
>> }
>
> That's not an interesting use of a local function! You can move square()
> outside, and it would still work, putting aside any clashes with
> existing names called 'square'.

Sure. It was not intended to be anything other than a very simple
nested function written in gcc C extension syntax, and the same thing
written in standard C++, to demonstrate how you can write nested
functions in C++ without this extension. Local functions without
capturing local data are perhaps not interesting, but they can be neat
from the viewpoint of limiting the scope of identifiers.

>
> The challenges of local functions are to do with accessing transient
> variables belonging to their enclosing function.

Yes. And you can do that with the gcc extension, and also with C++ lambdas.

> Especially when a
> reference to the local function is called from outside the lexical scope
> of the enclosing function, with extra difficulties when the enclosing
> function is no longer active.
>

These are the kinds of situations where the gcc extension needs to use
trampolines. C++ lambdas are closures, and can hold more information -
each lambda is its own type, and the size of that type can be different
for different lambdas (it can include pointers to the variables captured
by reference, for example). This makes lambdas much easier, safer and
more efficient to use within a translation unit - but makes them a bit
more fiddly if they capture local variables and you need to pass them to
something defined outside the current TU.

(You can't use any local function outside the scope of the enclosing
code if the local function has reference captures - that applies to all
types of local functions.)

>
>
>> int sum_square(int x, int y) {
>>      auto square = [](int z) {
>>          return z * z;
>>      }
>>      return square(x) + square(y);
>> }
>
> What's the significance of the '[]' here?
>

It's the syntax for a C++ lambda. You can put captures in there, or
leave it empty for no captures.

Just for your entertainment, with C++ lambdas this is now legal code:

void foo(void) {
[](){}();
}

(It defines a lambda with no captures and no parameters, that does
nothing, then it invokes it.)

Re: Recursion, Yo

<20240410063255.565@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35022&group=comp.lang.c#35022

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 17:10:40 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <20240410063255.565@kylheku.com>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
<uv4r9e$mdd3$1@dont-email.me> <uv5e3l$q885$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Apr 2024 19:10:40 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a02df6af16e190f6c7ee895d512b8f84";
logging-data="1151493"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iNroKgaxamAlKQa3SGLIr1Ph4pcdOAFo="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:RwUxmemz6V7G1pQXQWyaHm99AYI=
 by: Kaz Kylheku - Wed, 10 Apr 2024 17:10 UTC

On 2024-04-10, David Brown <david.brown@hesbynett.no> wrote:
> On 10/04/2024 03:50, Lawrence D'Oliveiro wrote:
>> On Tue, 09 Apr 2024 11:44:23 +0100, Ben Bacarisse wrote:
>>
>>> It's significant (or at least note worthy) that the code in the
>>> original post was not ISO C ...
>>
>> Interesting that GCC’s C compiler allows nested routine definitions,
>> but the C++ compiler does not.
>
> It is an old extension, going back to when gcc barely (if at all)
> supported C++. The compiler middle-end had to have support for nested
> functions for languages like Pascal, Module 2 and Ada (I believe Ada is
> the only one of these that stuck around in gcc mainline, but other
> language front-ends have been made outside the main tree). Someone
> thought it might be a useful feature in C too, and perhaps something
> that would catch on in the standards (several early gcc extensions ended
> up standardised in C99).
>
> It is not much used in practice, AFAIK. For some cases the code
> generation for nested functions was fine and straight-forward. In other
> cases, however, it required a trampoline generated on the stack, and
> that became a real pain once non-executable stacks came into fashion.
>
> Nested functions were never as interesting for C++ as you already have
> better mechanisms for controlling scope and data access - classes and
> their methods, including nested classes. And once lambdas joined the

Higher level languages like Common Lisp show that you want both;
even though theoretically you can implement OOP with lambdas, or
simulate some aspects of lambdas with OOP (particularly if your OOP
supports callable objects).

> party in C++11 there was absolutely no reason to have nested functions -
> there is nothing (AFAIK) that you could do with nested functions that
> you can't do at least as well, and often better, with lambdas.

GNU C nested functions don't require any special syntax (other than teh
fact of allowing a function definition inside a statement block). They
implicitly capture the actual variables in the surrounding scope using
ordinary C function syntax.

The address of a GNU C nested function is regular function pointer;
you can pass it to qsort and bsearch.

In fact, you can pass a GNU C nested function into a library that was
not even compiled with GCC. If it understand the calling conventions of
function pointers, it can call the function.

However, GNU C nested functions are "downward funarg" only, which
is a pretty severe limitation. (Worse, GNU C nested function can
escape from a scope in spite of being dowward funarg only,
causing undefined behavior when they are called.)

(The "downward funarg" terminology is from ancient Lisp or Algol
literature, means "downward (only) functional argument": a function that
can be used as an argument to function, but not returned; i.e. passed
downward only.)

The downward funarg restriction has benefits too; such functions do not
require any dynamic allocation at all. The closed-over variables are
referenced directly in their still-living stack frame.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Recursion, Yo

<86cyqx16p2.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35023&group=comp.lang.c#35023

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 10:14:01 -0700
Organization: A noiseless patient Spider
Lines: 142
Message-ID: <86cyqx16p2.fsf@linuxsc.com>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org> <uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk> <uv4r9e$mdd3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Wed, 10 Apr 2024 19:14:03 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5ec1c0fb8572bf9a2af34710c5b65b61";
logging-data="1153169"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/XoCz5xSZ97t+vu2y8Wnrpv2ghKU9Ge34="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:/rqkl0CRnmwiOCav2mj5haEqGBs=
sha1:2X4d97gERzP2amSobOnYct3EgSo=
 by: Tim Rentsch - Wed, 10 Apr 2024 17:14 UTC

Lawrence D'Oliveiro <ldo@nz.invalid> writes:

> On Tue, 09 Apr 2024 11:44:23 +0100, Ben Bacarisse wrote:
>
>> It's significant (or at least note worthy) that the code in the
>> original post was not ISO C ...
>
> Interesting that GCC?s C compiler allows nested routine definitions,
> but the C++ compiler does not.
>
>> and re-writing it in ISO C would show up some of the issues involved
>> ...
>
> Ask and you shall receive ...
>
> /*
> Generate permutations of a list of items.
> Pass a list of arbitary words as command arguments, and this
> program will print out all possible orderings of them.
> */
>
> #include <iso646.h>
> #include <stdbool.h>
> #include <stdlib.h>
> #include <stdio.h>
>
> typedef void (*permute_action)
> (
> unsigned int nrwords,
> const char * const * words,
> void * arg
> );
>
> struct permute_ctx
> {
> unsigned int nrwords;
> const char * const * words;
> permute_action action;
> void * action_arg;
> const char ** permbuf;
> bool * used;
> } /*permute_ctx*/;
>
> static void permute1
> (
> struct permute_ctx * ctx,
> unsigned int depth
> )
> {
> if (depth < ctx->nrwords)
> {
> for (unsigned int i = 0; i < ctx->nrwords; ++i)
> {
> if (not ctx->used[i])
> {
> ctx->permbuf[depth] = ctx->words[i];
> ctx->used[i] = true;
> permute1(ctx, depth + 1);
> ctx->used[i] = false;
> } /*if*/
> } /*for*/
> }
> else
> {
> ctx->action(ctx->nrwords, ctx->permbuf, ctx->action_arg);
> } /*if*/
> } /*permute1*/
>
> void permute
> (
> unsigned int nrwords,
> const char * const * words,
> permute_action action,
> void * action_arg
> )
> {
> if (nrwords > 0)
> {
> struct permute_ctx ctx;
> ctx.nrwords = nrwords;
> ctx.words = words;
> ctx.action = action;
> ctx.action_arg = action_arg;
> ctx.permbuf = (const char **)malloc(nrwords * sizeof(char *));
> ctx.used = (bool *)malloc(nrwords);
> for (unsigned int i = 0; i < nrwords; ++i)
> {
> ctx.used[i] = false;
> } /*for*/
>
> permute1(&ctx, 0);
>
> free(ctx.permbuf);
> free(ctx.used);
> } /*if*/
> } /*permute*/
>
> [...]

More complicated than it needs to be. Besides that, in a beauty
contest this coding style would lose out even to Frankenstein.

Simpler:

#include <stdlib.h>
#include <string.h>

typedef void *Voidp;
typedef struct array_CB_s ArrayCB;
struct array_CB_s {
void (*run)( ArrayCB *cb, unsigned n, const Voidp[ n ] );
};

static void permute_r( unsigned n, Voidp[n], unsigned, ArrayCB * );
static void voidp_exchange( Voidp [], unsigned, unsigned );

void
permute( unsigned n, const Voidp in_array[n], ArrayCB *cb ){
for( Voidp *pva = malloc( n * sizeof *pva ); pva; pva = 0 ){
memcpy( pva, in_array, n * sizeof *pva );
permute_r( n, pva, n, cb );
free( pva );
}
}

void
permute_r( unsigned n, Voidp values[n], unsigned k, ArrayCB *cb ){
for( unsigned i = 0; i < k; i++ ){
voidp_exchange( values, n-k, n-k+i );
permute_r( n, values, k-1, cb );
voidp_exchange( values, n-k, n-k+i );
}
if( k == 1 ) cb->run( cb, n, values );
}

void
voidp_exchange( Voidp values[], unsigned i, unsigned j ){
if( i != j ){
Voidp pi = values[i], pj = values[j];
values[i] = pj, values[j] = pi;
}
}

Re: Recursion, Yo

<20240410101126.236@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=35024&group=comp.lang.c#35024

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Recursion, Yo
Date: Wed, 10 Apr 2024 17:19:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <20240410101126.236@kylheku.com>
References: <uut24f$2icpb$1@dont-email.me> <uutqd2$bhl0$1@i2pn2.org>
<uv2u2a$41j5$1@dont-email.me> <87edbestmg.fsf@bsb.me.uk>
<uv4r9e$mdd3$1@dont-email.me> <uv5e3l$q885$1@dont-email.me>
<uv5gfd$qum1$1@dont-email.me> <uv5lgl$s6uj$1@dont-email.me>
Injection-Date: Wed, 10 Apr 2024 19:19:31 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a02df6af16e190f6c7ee895d512b8f84";
logging-data="1151493"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18a2UmQWLOjQrV3tRsxRzrmsOZqXWM10dw="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:u6/Am1YVO7qI6KqECJffAMkcrpA=
 by: Kaz Kylheku - Wed, 10 Apr 2024 17:19 UTC

On 2024-04-10, David Brown <david.brown@hesbynett.no> wrote:
> Basically, anything you could do with a nested function in gcc C you can
> do in C++:

Except pass that lambda to another module that expects a simple function
pointer.

That's not just C libraries. There is a foreign function ecosystem based
on the conventions of C. If you have a function pointer, you can pass it
into any high level language that supports C interpo, and it can call
that thing.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Pages:12345
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor