[erlang-questions] Nested for-loops, there has to be a better way to do this

Cláudio Amaral coa@REDACTED
Wed Jul 29 22:20:51 CEST 2009


Brentley Jones escreveu:
> On Jul 29, 2009, at 2:13 PM, Zoltan Lajos Kis wrote:
>
>> Brentley Jones wrote:
>>>
>>> On Jul 29, 2009, at 1:57 PM, Zoltan Lajos Kis wrote:
>>>
>>>> And what do I gain using this for function compared to simply 
>>>> writing an "ad-hoc" recursive function whenever needed ?
>>>
>>> Nothing is gained by my bloated, yet modular function. I was just 
>>> expanding on the modular function listed before mine.
>>>
>>>> PS: seriously, is this what the original question was getting at?
>>>
>>> I do think it shows how to do nested for loops in a way that looks 
>>> very imperative, which is what I think the original question was 
>>> getting at.
>>>
>>> On Jul 29, 2009, at 2:55 PM, Yves S. Garret wrote:
>>>
>>>> I was playing around with the idea of implementing a nested for-loop
>>>> like construct (I will concede, they are evil, but at rare times,
>>>> necessary.)  In non-functional programming languages, they are trivial
>>>> to do.  However, this has proved to be one of those simple things that
>>>> are a pain in the neck for me.
>> But if you look just one sentence further...
>> "The idea was to be able to pass in a list [5, 5, 10] (for example) 
>> and go through it all like 3 dimensional array."
>
> The fact that he says "in non-functional programming languages, they 
> are trivial to do." just lead me to try to think that way. In a 
> non-functional programming language there isn't a construct that he 
> can just pass a list which has lengths for each dimension, he would 
> have to set up each for loop with those values as the conditionals.
>
I'm with you in this...

In the end, it should depend on the purpose of the program.

So, if you are ok with rewriting your 'loops' when needed (making them 
more readable and/or efficient), the code can be much simpler (like  the 
code from Richard O'Keefe).

If you are interested in writing some function to use like an imperative 
'for', you have to take into account the possibility of non-integer 
index term (and step function), a flexible body representation to allow 
any argument passing and simulate the state changes between iterations 
and the the hard job to the one calling the 'for' function

>> Anyway, we should get the answer by dawn (CET) :)
>>
>> Z.
>
>
> I think with the resources that we have provided though that we have 
> answered the question in one way or another (either with these modular 
> for loop constructs or the nice list comprehensions , etc).
>
> And Joe asked a very good question, what does he want the output to be?
> Does he want a body that has access to i, j, k [5, 5, 10] and is 
> called like three nested for-loops (which is what I thought), or 
> something totally different?
>
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>



More information about the erlang-questions mailing list