-
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
210 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,111 @@ | ||
defmodule LittleAlchemist.Chapter04 do | ||
|
||
def zero?(n), do: n == 0 | ||
|
||
def add1(n), do: n + 1 | ||
def sub1(n), do: n - 1 | ||
|
||
# def add(n, m) do | ||
# cond do | ||
# zero?(m) -> n | ||
# true -> add1(sub1(m) + n) | ||
# end | ||
# end | ||
|
||
def add(n, 0), do: n | ||
def add(n, m), do: 1 + add(n, m - 1) | ||
|
||
|
||
def sub(n, 0), do: n | ||
def sub(n, m), do: sub(n, m - 1) - 1 | ||
|
||
|
||
def addtup([]), do: 0 | ||
def addtup([h | t]), do: add(h, addtup(t)) | ||
|
||
def addtup(t1, []), do: t1 | ||
def addtup([], t2), do: t2 | ||
def addtup([h1|t1], [h2|t2]), do: [h1 + h2 | addtup(t1, t2)] | ||
|
||
# def addtup([], l), do: l | ||
# def addtup(l, []), do: l | ||
# def addtup(a, b), do: cons(add(car(a), car(b)), addtup(cdr(a), cdr(b))) | ||
|
||
def mul(n, 1), do: n | ||
def mul(n, m), do: n + mul(n, m-1) | ||
|
||
|
||
def greater_than(0, _), do: false | ||
def greater_than(_, 0), do: true | ||
def greater_than(x, y), do: greater_than(sub1(x), sub1(y)) | ||
|
||
def less_than(_, 0), do: false | ||
def less_than(0, _), do: true | ||
def less_than(x, y), do: less_than(sub1(x), sub1(y)) | ||
|
||
|
||
# def equals(n, m) do | ||
# cond do | ||
# greater_than(n, m) -> false | ||
# less_than(n, m) -> false | ||
# true -> true | ||
# end | ||
# end | ||
|
||
def equals(0, 0), do: true | ||
def equals(_, 0), do: false | ||
def equals(0, _), do: false | ||
def equals(n, m), do: equals(n-1, m-1) | ||
|
||
def power(_, 0), do: 1 | ||
def power(n, m), do: mul(n, power(n, sub1(m))) | ||
|
||
# def power(n, 1), do: n | ||
# def power(n, m), do: n * power(n, m-1) | ||
|
||
# def divide(n, m) do | ||
# cond do | ||
# less_than(n, m) -> 0 | ||
# true -> add1(divide(sub(n, m), m)) | ||
# end | ||
# end | ||
|
||
def divide(n, m) when n < m, do: 0 | ||
def divide(n, m), do: 1 + divide(n-m, m) | ||
|
||
def size([]), do: 0 | ||
def size([_ | t]), do: 1 + size(t) | ||
|
||
# def pick(n, lat) do | ||
# cond do | ||
# equals(sub1(n), 0) -> car(lat) | ||
# true -> pick(sub1(n), cdr(lat)) | ||
# end | ||
# end | ||
|
||
def pick(1, [h | _]), do: h | ||
def pick(n, [_ | t]), do: pick(n - 1, t) | ||
|
||
def rempick(1, [_ | t]), do: t | ||
def rempick(n, [h | t]), do: [h | rempick(n-1, t)] | ||
|
||
|
||
def nonums([]), do: [] | ||
def nonums([h | t]) when is_number(h), do: nonums(t) | ||
def nonums([h | t]), do: [h | nonums(t)] | ||
|
||
def allnums([]), do: [] | ||
def allnums([h | t]) when not is_number(h), do: allnums(t) | ||
def allnums([h | t]), do: [h | allnums(t)] | ||
|
||
def equan(a, a), do: true | ||
def equan(_, _), do: false | ||
|
||
def occur(_, []), do: 0 | ||
def occur(a, [a | t]), do: 1 + occur(a, t) | ||
def occur(a, [_ | t]), do: occur(a, t) | ||
|
||
def one?(1), do: true | ||
def one?(_), do: false | ||
|
||
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,99 @@ | ||
defmodule LittleAlchemist.Chapter04Test do | ||
use ExUnit.Case | ||
import LittleAlchemist.Chapter04 | ||
|
||
test "add1" do | ||
assert add1(67) == 68 | ||
end | ||
|
||
test "zero?" do | ||
assert zero?(0) | ||
assert zero?(42) == false | ||
end | ||
|
||
test "add" do | ||
assert add(46, 12) == 58 | ||
end | ||
|
||
test "sub" do | ||
assert sub(14, 3) == 11 | ||
assert sub(17, 9) == 8 | ||
end | ||
|
||
test "addtup" do | ||
assert addtup([3, 5, 2, 8]) == 18 | ||
assert addtup([15, 6, 7, 12, 3]) == 43 | ||
assert addtup([3, 7], [4, 6, 8, 1]) == [7, 13, 8, 1] | ||
assert addtup([3, 6, 9], [8, 5, 2, 0, 7]) == [11, 11, 11, 0, 7] | ||
assert addtup([3, 7, 8], [1, 1, 1]) == [4, 8, 9] | ||
end | ||
|
||
test "mul" do | ||
assert mul(5, 3) == 15 | ||
assert mul(13, 4) == 52 | ||
end | ||
|
||
test "greater_than" do | ||
refute greater_than(12, 133) | ||
assert greater_than(120, 11) | ||
end | ||
|
||
test "less_than" do | ||
refute less_than(4, 2) | ||
assert less_than(2, 4) | ||
refute less_than(0, 0) | ||
end | ||
|
||
test "equals" do | ||
refute equals(2, 3) | ||
assert equals(9, 9) | ||
end | ||
|
||
test "power" do | ||
assert power(1, 1) == 1 | ||
assert power(2, 3) == 8 | ||
assert power(5, 3) == 125 | ||
end | ||
|
||
test "divide" do | ||
assert divide(5, 3) == 1 | ||
assert divide(7, 3) == 2 | ||
end | ||
|
||
test "size" do | ||
assert size([:hotdogs, :with, :mustard, :sauerkraut, :and, :pickes]) == 6 | ||
assert size([:ham, :and, :cheese, :on, :rye]) == 5 | ||
end | ||
|
||
test "pick" do | ||
assert pick(4, [:lasagna, :spaghetti, :ravioli, :macaroni, :meatball]) == :macaroni | ||
end | ||
|
||
test "rempick" do | ||
assert rempick(3, [:hotdogs, :with, :hot, :mustard]) == [:hotdogs, :with, :mustard] | ||
end | ||
|
||
test "nonums" do | ||
assert nonums([5, :pears, 6, :prunes, 9, :dates]) == [:pears, :prunes, :dates] | ||
end | ||
|
||
test "allnums" do | ||
assert allnums([5, :pears, 6, :prunes, 9, :dates]) == [5, 6, 9] | ||
end | ||
|
||
test "equan" do | ||
refute equan(:a, :b) | ||
assert equan(:z, :z) | ||
end | ||
|
||
test "occur" do | ||
assert occur(:c, [:a, :c, :d, :c]) == 2 | ||
assert occur(:c, [:z, :z, :top]) == 0 | ||
end | ||
|
||
test "one" do | ||
assert one?(1) | ||
refute one?(9) | ||
end | ||
|
||
end |