-
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
178 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,46 @@ | ||
defmodule LittleAlchemist.Chapter05 do | ||
import LittleAlchemist.Chapter01 | ||
|
||
def rember_star(_, []), do: [] | ||
def rember_star(a, [a | t]), do: rember_star(a, t) | ||
def rember_star(a, [h | t]) when is_atom(h), do: [h | rember_star(a, t)] | ||
def rember_star(a, [h | t]), do: [rember_star(a, h) | rember_star(a, t)] | ||
|
||
def insertR_star(_, _, []), do: [] | ||
def insertR_star(n, o, [o | t]), do: [o, n | insertR_star(n, o, t)] | ||
def insertR_star(n, o, [h | t]) when is_atom(h), do: [h | insertR_star(n, o, t)] | ||
def insertR_star(n, o, [h | t]), do: [insertR_star(n, o, h) | insertR_star(n, o, t)] | ||
|
||
def occur_star(_, []), do: 0 | ||
def occur_star(a, [a | t]), do: 1 + occur_star(a, t) | ||
def occur_star(a, [h | t]) when is_atom(h), do: occur_star(a, t) | ||
def occur_star(a, [h | t]), do: occur_star(a, h) + occur_star(a, t) | ||
|
||
def subst_star(_, _, []), do: [] | ||
def subst_star(n, o, [o | t]), do: [n | subst_star(n, o, t)] | ||
def subst_star(n, o, [h | t]) when is_atom(h), do: [h | subst_star(n, o, t)] | ||
def subst_star(n, o, [h | t]), do: [subst_star(n, o, h) | subst_star(n, o, t)] | ||
|
||
def insertL_star(_, _, []), do: [] | ||
def insertL_star(n, o, [o | t]), do: [n, o | insertL_star(n, o, t)] | ||
def insertL_star(n, o, [h | t]) when is_atom(h), do: [h | insertL_star(n, o, t)] | ||
def insertL_star(n, o, [h | t]), do: [insertL_star(n, o, h) | insertL_star(n, o, t)] | ||
|
||
def member_star(_, []), do: false | ||
def member_star(a, [a | _]), do: true | ||
def member_star(a, [h | t]) when is_atom(h), do: member_star(a, t) | ||
def member_star(a, [h | t]), do: member_star(a, h) || member_star(a, t) | ||
|
||
def leftmost([]), do: [] | ||
def leftmost([h | _]) when is_atom(h), do: h | ||
def leftmost([h | _]), do: leftmost(h) | ||
|
||
def eqlist?([], []), do: true | ||
def eqlist?([], _), do: false | ||
def eqlist?(_, []), do: false | ||
def eqlist?([h1 | _], [h2 | _]) when is_atom(h1) and not is_atom(h2), do: false | ||
def eqlist?([h1 | _], [h2 | _]) when not is_atom(h1) and is_atom(h2), do: false | ||
def eqlist?([h1 | t1], [h2 | t2]) when is_atom(h1) and is_atom(h2), do: h1 == h2 && eqlist?(t1,t2) | ||
def eqlist?([h1 | t1], [h2 | t2]), do: eqlist?(h1, h2) && eqlist?(t1,t2) | ||
|
||
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,132 @@ | ||
defmodule LittleAlchemist.Chapter05Test do | ||
use ExUnit.Case | ||
import LittleAlchemist.Chapter05 | ||
|
||
|
||
test "rember_star" do | ||
assert rember_star(:cup, [ | ||
[:coffee], :cup, [[:tea], :cup], [:and, [:hick]], :cup | ||
]) == [[:coffee], [[:tea]], [:and, [:hick]]] | ||
|
||
assert rember_star(:sauce, [ | ||
[[:tomato, :sauce]], | ||
[[:bean], :sauce], | ||
[:and, [[:flying]], :sauce] | ||
]) == [ | ||
[[:tomato]], | ||
[[:bean]], | ||
[:and, [[:flying]]] | ||
] | ||
end | ||
|
||
test "insertR_star" do | ||
assert insertR_star(:roast, :chuck, [ | ||
[:how, :much, [:wood]], | ||
:could, | ||
[[:a, [:wood], :chuck]], | ||
[[[:chuck]]], | ||
[:if, [:a], [[:wood, :chuck]]], | ||
:could, :chuck, :wood | ||
]) == [ | ||
[:how, :much, [:wood]], | ||
:could, | ||
[[:a, [:wood], :chuck, :roast]], | ||
[[[:chuck, :roast]]], | ||
[:if, [:a], [[:wood, :chuck, :roast]]], | ||
:could, :chuck, :roast, :wood | ||
] | ||
end | ||
|
||
test "occur_star" do | ||
assert occur_star(:banana, [ | ||
[:banana], | ||
[:split, [ | ||
[[[:banana, :ice]]], | ||
[:cream, [:banana]], | ||
:sherbet | ||
]], | ||
[:banana], | ||
[:bread], | ||
[:banana, :brandy] | ||
]) == 5 | ||
end | ||
|
||
test "subst_star" do | ||
assert subst_star(:orange, :banana, [ | ||
[:banana], | ||
[:split, [ | ||
[[[:banana, :ice]]], | ||
[:cream, [:banana]], | ||
:sherbet | ||
]], | ||
[:banana], | ||
[:bread], | ||
[:banana, :brandy] | ||
]) == [ | ||
[:orange], | ||
[:split, [ | ||
[[[:orange, :ice]]], | ||
[:cream, [:orange]], | ||
:sherbet | ||
]], | ||
[:orange], | ||
[:bread], | ||
[:orange, :brandy] | ||
] | ||
end | ||
|
||
test "insertL_star" do | ||
assert insertL_star(:pecker, :chuck, [ | ||
[:how, :much, [:wood]], | ||
:could, | ||
[[:a, [:wood], :chuck]], | ||
[[[:chuck]]], | ||
[:if, [:a], [[:wood, :chuck]]], | ||
:could, :chuck, :wood | ||
]) == [ | ||
[:how, :much, [:wood]], | ||
:could, | ||
[[:a, [:wood], :pecker, :chuck]], | ||
[[[:pecker, :chuck]]], | ||
[:if, [:a], [[:wood, :pecker, :chuck]]], | ||
:could, :pecker, :chuck, :wood | ||
] | ||
end | ||
|
||
test "member_star" do | ||
assert member_star(:chips, [ | ||
[:potato], [:chips, [[:with], :fish], [:chips]] | ||
]) | ||
end | ||
|
||
test "leftmost" do | ||
assert leftmost([ | ||
[:potato], | ||
[:chips, [[:with], :fish], [:chips]] | ||
]) == :potato | ||
end | ||
|
||
test "eqlist?" do | ||
assert eqlist?( | ||
[:strawberry, :ice, :cream], | ||
[:strawberry, :ice, :cream] | ||
) | ||
refute eqlist?( | ||
[:strawberry, :ice, :cream], | ||
[:strawberry, :cream, :ice] | ||
) | ||
refute eqlist?( | ||
[:banana, [[:split]]], | ||
[[:banana], [:split]] | ||
) | ||
refute eqlist?( | ||
[:beef, [[:sausage]], [:and, [:soda]]], | ||
[:beef, [[:salami]], [:and, [:soda]]] | ||
) | ||
assert eqlist?( | ||
[:beef, [[:sausage]], [:and, [:soda]]], | ||
[:beef, [[:sausage]], [:and, [:soda]]] | ||
) | ||
end | ||
|
||
end |