Skip to content

Commit

Permalink
chapter 6
Browse files Browse the repository at this point in the history
  • Loading branch information
emadb committed Nov 3, 2023
1 parent 2c2d78f commit c384f88
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/chapter_06.ex
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
35 changes: 35 additions & 0 deletions test/chapter_06_test.exs
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

0 comments on commit c384f88

Please sign in to comment.