Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Sort of trivial code challenge - may be interesting to you anyway
Date: Wed, 04 Mar 2026 08:09:43 -0800
Organization: A noiseless patient Spider
Lines: 118
Message-ID: <86ldg7d1a0.fsf@linuxsc.com>
References: <10n80sc$3soe4$1@dont-email.me> <86v7feei2e.fsf@linuxsc.com> <10o53k6$1i0ef$2@dont-email.me> <86ms0peby6.fsf@linuxsc.com> <10o6nci$21m76$1@dont-email.me> <86a4wpdmfi.fsf@linuxsc.com> <10o9c3n$2ul3p$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Wed, 04 Mar 2026 16:09:48 +0000 (UTC)
Injection-Info: dont-email.me; posting-host="de0045d7c780f08ea604ccdb8e034471"; logging-data="3196201"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nfdfYUs10oIiuDfWrBLhv/4a1ZajeDUA="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:b4928SQJuOdJ0Mra1pi+47GW6Qo= sha1:2XJB2SAHOADEWEV97eNckdcZwBM=
Xref: csiph.com comp.lang.c:396763
DFS writes:
> On 3/3/2026 9:20 AM, Tim Rentsch wrote:
>
>> DFS writes:
>>
>>> On 3/3/2026 12:09 AM, Tim Rentsch wrote:
>>>
>>>> DFS writes:
>>>>
>>>>> On 3/2/2026 3:44 AM, Tim Rentsch wrote:
>>>>>
>>>>>> DFS writes:
>>>>>>
>>>>>>
>>>>>> A straightfoward exercise. Here is a counter challenge to make
>>>>>> things more interesting: as above, but don't use nested loops
>>>>>> (or goto's, etc).
>>>>>
>>>>> Done.
>>
>> I should have added, I appreciate your taking on the challenge.
>
> Absolutely. It was actually interesting to get it done without loops.
>
>>> Recursion means the function calls itself.
>>
>> What you're describing is called direct recursion. The word
>> recursion by itself, without the adjective, includes the case
>> of mutually recursive functions.
>
> I see.
>
> I didn't realize there were so many types of recursion:
>
> direct: tail, head, tree, nested
>
> indirect (or mutual), linear, multiple, structural, generative.
Just a note that a function can be tail recursive without being
directly recursive. A tail call is one where the result of the call
is the return value of the calling function, regardless of whether
the call is recursive, including being indirectly recursive.
>>> [...]
>>>
>>>> The latest challenge, which I just got through doing, is to
>>>> disallow if, for, while, goto, return, and to forbid functions
>>>> and function calls except for calls to C standard library
>>>> functions.
>>>
>>> Yikes!
>>>
>>> Is main() OK?
>>
>> Yes, sorry, not mentioning main() was an oversight on my part.
>> (Still not okay to call it.)
>
> I was actually kidding, but I see online you can do some trickery to
> make a standalone C program work without main().
To me that would make the problem outside the realm of C programs,
and so subject to a technical out-of-bounds in this newsgroup. I
know other people have different stances on this question, I am
simply explaining my own view so you know where I'm coming from.
>>> [...]
>>>
>>> I note that the no-loops challenge was a worthwhile pursuit I never
>>> even considered.
>>
>> Yes, no loops is fun. Once you get used to it, using tail-recursive
>> functions in place of loops often seems like a better choice.
>
> Yes.
>
> for.. and while.. loops are more intuitive (because we likely spent
> many years using them), but once I got it working without them it felt
> sort of freeing.
Like what you say, for() and while() feel more natural because
you're more used to them. That will change as you use a functional
and/or recursive style more. My own experience with functional
programming and writing functions resursively is that at first they
seemed somewhat contrived but as I gained experience they felt more
natural, and later in many cases a functional/recursive writing
seemed easier and more direct. So I urge you to continue pushing
forward on this path.
>>> I think a recursive function could be really short and sweet, so I'm
>>> going to try that.
>>
>> By all means. The version I first wrote had two tail-recursive
>> functions, one to find the appropriate number of rows and columns
>> for a given cutoff, and one to show the matrix of values.
>
> I recently thought of a new approach: fill an array with 1 to the
> cutoff (min(cutoff, rows*cols) anyway), and just print the whole array
> col by row. Then there's never a need to check each value as you're
> printing it.
Hmmm. Well I give you points for originality. ;)
> For me a for.. loop is easiest to fill an array.
>
> but if loops are banned you could do it recursively.
>
> but if recursion is banned you could do it?
My hint is there are some powerful functions in the C standard
library that make this feasible.
If that hint isn't enough, someone else asked a question in this
thread (and I responded) that should point you in the right
direction.
If both of those hints aren't enough, ask again and I'll try to get
you closer to the goal.