回答編集履歴
1
より良い解決策の追記
answer
CHANGED
@@ -1,9 +1,33 @@
|
|
1
1
|
```Elixir
|
2
|
-
def
|
2
|
+
def MultiList(list1,list2) do
|
3
3
|
Enum.map(0..length(list1)-1,fn(n)->
|
4
4
|
Enum.at(list1,n) + Enum.at(list2,n)
|
5
5
|
end)
|
6
6
|
end
|
7
7
|
```
|
8
8
|
で一応できました.
|
9
|
-
他にもっと良い書き方がありましたら教えていただきけると嬉しいです.
|
9
|
+
他にもっと良い書き方がありましたら教えていただきけると嬉しいです.
|
10
|
+
|
11
|
+
----------追記------------
|
12
|
+
```Elixir
|
13
|
+
defmodule MultiList do
|
14
|
+
def sum(list1, list2, total \ [])
|
15
|
+
|
16
|
+
def sum([], [], total) do
|
17
|
+
Enum.reverse(total)
|
18
|
+
end
|
19
|
+
|
20
|
+
def sum([h1 | t1], [], total) do
|
21
|
+
sum(t1, [], [h1 | total])
|
22
|
+
end
|
23
|
+
|
24
|
+
def sum([], [h2 | t2], total) do
|
25
|
+
sum([], t2, [h2 | total])
|
26
|
+
end
|
27
|
+
|
28
|
+
def sum([h1 | t1], [h2 | t2], total) do
|
29
|
+
sum(t1, t2, [h1 + h2 | total])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
[Elixir Forum](https://elixirforum.com/t/id-like-to-take-the-element-of-the-same-index-from-multiple-lists/28102)にて、リストの長さが異なる場合にも対応した解決策を答えてもらったので追記します.
|