-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule LittleAlchemist.Chapter06 do | ||
import LittleAlchemist.Chapter04, only: [power: 2] | ||
|
||
def numbered?([left, op, right]) when op in [:+, :*, :^], | ||
do: numbered?(left) && numbered?(right) | ||
|
||
def numbered?(n), do: is_number(n) | ||
|
||
def value([left, :+, right]), do: value(left) + value(right) | ||
def value([left, :*, right]), do: value(left) * value(right) | ||
def value([left, :^, right]), do: power(value(left), value(right)) | ||
def value(n) when is_number(n), do: n | ||
|
||
def sero?([]), do: true | ||
def sero?(_), do: false | ||
|
||
def edd1(a), do: [[] | a] | ||
def zub1([_ | t]), do: t | ||
|
||
def edd(a, []), do: a | ||
def edd(a, [_ | t]), do: edd([[] | a], t) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule LittleAlchemist.Chapter06Test do | ||
use ExUnit.Case | ||
import LittleAlchemist.Chapter06 | ||
|
||
test "numbered?" do | ||
assert numbered?(2) | ||
refute numbered?(:a) | ||
assert numbered?([3, :+, [4, :*, 5]]) | ||
refute numbered?([3, :*, :sausage]) | ||
end | ||
|
||
test "value" do | ||
assert value(13) == 13 | ||
assert value([1, :+, 3]) == 4 | ||
assert value([1, :+, [3, :^, 4]]) == 82 | ||
end | ||
|
||
test "zero?" do | ||
assert sero?([]) | ||
end | ||
|
||
test "edd1" do | ||
assert edd1([]) == [[]] | ||
assert edd1([[]]) == [[], []] | ||
end | ||
|
||
test "zub1" do | ||
assert zub1([[], []]) == [[]] | ||
assert zub1([[]]) == [] | ||
end | ||
|
||
test "edd" do | ||
assert edd([[], []], [[], [], []]) == [[], [], [], [], []] | ||
end | ||
end |