Since one month, I'm learning Erlang and I like it so much. Today, I wrote an algorithm to solve a problem. Some time ago, I used two nested for loops in C# to solve it but, solving the same problem in Erlang, I asked myself if there is a better way to write something similar to the two nested for loops I used before.

Let's say that the two nested for loops in C# are:

for(int i = 100; i<=999;i++)
        for(int j = 100;j<=999;j++)
            int prod = i*j;
              //some logic here..

and the respective code in Erlang is the following:

loopINumber(_,LowerLim,UpperLim,Product) when LowerLim > UpperLim -> {Product};

loopINumber(ILimit,LowerLim,UpperLim,Product) ->
    Product = loopJNumber(ILimit,UpperLim,LowerLim,Product),

loopJNumber(LowerLim,UpperLim,_,Product) when LowerLim > UpperLim -> Product;

loopJNumber(LowerLim,UpperLim,ILimit,Product) ->
    Product = ILimit * LowerLim,
    %% some logic here..

start(ILimit,JLimit) -> loopINumber(ILimit,ILimit,JLimit,0).

I know that maybe there's something to improve. Being still a beginner in functional languages, most probably I'm still coding in a "sequential" or OOP way. How can I improve this Erlang code?


2 Answers 2


In general, nested for loops translate well to list comprehensions:

Prods = [I*J || I <- lists:seq(100,999), J <- lists:seq(100,999)]
// Generates a list of products to perform further logic on
  • \$\begingroup\$ Yeah, the C# version is really bad. This is what I would write: var threeDigitNumbers = Enumerable.Range(100, 899); var palindromes = threeDigitNumbers.Zip(threeDigitNumbers, (x, y) => x * y ).Where(n => n.ToString() == new string(n.ToString().Reverse().ToArray())); This matches pretty well to your Erlang proposal. \$\endgroup\$ Jun 15, 2014 at 23:56
  • 2
    \$\begingroup\$ The hazard with list comprehensions is that you have to generate the entire list, so an arbitrarily-large number of iterations will require an arbitrarily-large amount of memory. If the problem is bounded and the size of the lists is small enough that you're comfortable with the memory consumption, this is the way to go. \$\endgroup\$
    – Blrfl
    Jun 16, 2014 at 4:32
  • \$\begingroup\$ @Blrfl, most languages with list comprehensions either let you specify lazy evaluation somehow or do it by default. That lets you treat it like an arbitrarily-large, even infinite, data structure while only actually using memory for one element at a time. \$\endgroup\$ Jun 16, 2014 at 13:05
  • \$\begingroup\$ @KarlBielefeldt: Erlang evaluates lists in full before they're used. You can do lazy evaluation in a way that looks a bit like what the OP did, but you can't do it with a list comprehension. \$\endgroup\$
    – Blrfl
    Jun 16, 2014 at 13:48
  • \$\begingroup\$ @KarlBielefeldt Thank you for the answer. It seems correct and reasonable for me, I'll check that later and I'll let you know! :-) \$\endgroup\$ Jun 17, 2014 at 7:33

To limit the problem of memory consumption with list comprehension, you can use lists:foreach/2 (note that you cannot accumulate a result with foreach/2, if necessary you can use foldl/3):

    fun(X) -> lists:foreach(
                  fun(Y) -> io:format("~p,~p~n",[X,Y]) end,
                  lists:seq(1,5)) end,

or this version without prepared list, and the logic defined outside (works only since R17)

1> MyLogic = fun (init,init,init) -> []; (X,Y,R) -> [{X,Y}|R] end.
2> F = fun(X1,X2,Y1,Y2,Logic) -> 
           F1 = fun F1(X,R) when X == X2+1 -> R;
                    F1(X,R) -> F2 = fun F2(Y,Ry) when Y == Y2+1 -> Ry;
                                        F2(Y,Ry) -> 
                                            %% your logic here, for example
                                            NR = Logic(X,Y,Ry), 
3> F(1,3,21,22,MyLogic).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.