Scala版の焼き直しです。
Elixirっぽさがでているかどうかは微妙。
問題2
Problem2.ex
defmodule Problem2 do
def mutually_list(a, b) do
Enum.zip(a,b) |> Enum.flat_map(fn {x,y} -> [x,y] end)
end
end
問題3
Problem3.ex
defmodule Problem3 do
def fib do
Stream.unfold({0, 1}, fn {a,b} -> {a , {b, a + b}} end)
|> Enum.take(100)
end
end
問題4
Problem4.ex
defmodule Problem4 do
def permutations([]), do: [[]]
def permutations(list) do
for h <- list, t <- permutations(list -- [h]), do: [h | t]
end
def max(list) do
permutations(list)
|> Enum.map(fn p -> Enum.map(p, fn i -> Integer.to_string(i) end) |> List.to_string end)
|> Enum.max
end
end
問題5
Problem5.ex
defmodule Problem5 do
def calc(str, acc) do
case {Regex.run(~r/(^[1-9]+)(.*)/, str), Regex.run(~r/([+-])([1-9]+)(.*)/, str)} do
{[_, n, last], _} -> calc(last, acc + String.to_integer(n))
{_, [_, "+", n, last]} -> calc(last, acc + String.to_integer(n))
{_, [_, "-", n, last]} -> calc(last, acc - String.to_integer(n))
_ -> acc
end
end
def comb do
Enum.to_list(2..9)
|> List.foldl(["1"], fn (x, acc) -> Enum.flat_map(acc, fn n -> ["#{n}#{x}", "#{n}+#{x}", "#{n}-#{x}"] end) end)
end
def find do
Enum.filter(comb, fn str -> calc(str, 0) == 100 end)
end
end