0

A = [1,2,3]

The output should be:

[1,one,2,two,3,three]

Is it possible to get such an output?

Joseph Cho
  • 4,033
  • 4
  • 26
  • 33
Abhay
  • 25
  • 6

2 Answers2

5

Yes, you can put two clauses in the comprehension and make the second one return two elements for each element in the first one:

1> F = fun
1>   (1) -> "one";
1>   (2) -> "two";
1>   (3) -> "three"
1> end.
#Fun<erl_eval.6.127694169>
2> [B || A <- [1, 2, 3], B <- [A, F(A)]].
[1,"one",2,"two",3,"three"]
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • Thanks a lot !! So there needs to be some sort of mapping or use of a user-defined function to make this work right? – Abhay Sep 20 '18 at 19:45
  • @Abhay, Of course! The atom `three` has no inherent relationship to the integer `3`. – 7stud Sep 21 '18 at 04:32
0

i think the out put of last code would be

 [[1 ,"one"] , [2, "two"] , [3 , "tree"]].

some right answer will be

  lists:append([begin T = case I of
                            1 -> "One" ;
                            2->"Two";
                            3->"Three"; 
                            _ -> "" end ,
                      [I ,T] end
               || I <- lists:seq(1,3)]).
Mo Ein
  • 47
  • 5