🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # tam's solution

## to Zipper in the Erlang Track

Published at Aug 11 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Creating a zipper for a binary tree.

Zippers are a purely functional way of navigating within a data structure and manipulating it. They essentially contain a data structure and a pointer into that data structure (called the focus).

For example given a rose tree (where each node contains a value and a list of child nodes) a zipper might support these operations:

• `from_tree` (get a zipper out of a rose tree, the focus is on the root node)
• `to_tree` (get the rose tree out of the zipper)
• `value` (get the value of the focus node)
• `prev` (move the focus to the previous child of the same parent, returns a new zipper)
• `next` (move the focus to the next child of the same parent, returns a new zipper)
• `up` (move the focus to the parent, returns a new zipper)
• `set_value` (set the value of the focus node, returns a new zipper)
• `insert_before` (insert a new subtree before the focus node, it becomes the `prev` of the focus node, returns a new zipper)
• `insert_after` (insert a new subtree after the focus node, it becomes the `next` of the focus node, returns a new zipper)
• `delete` (removes the focus node and all subtrees, focus moves to the `next` node if possible otherwise to the `prev` node if possible, otherwise to the parent node, returns a new zipper)

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

### Test versioning

Each problem defines a macro `TEST_VERSION` in the test file and verifies that the solution defines and exports a function `test_version` returning that same value.

To make tests pass, add the following to your solution:

``````-export([test_version/0]).

test_version() ->
1.
``````

The benefit of this is that reviewers can see against which test version an iteration was written if, for example, a previously posted solution does not solve the current problem or passes current tests.

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### zipper_tests.erl

``````-module(zipper_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

%% Fixtures and helpers

bt(V, L, R) -> zipper:new_tree(V, L, R).
empty() -> nil.
leaf(V) -> zipper:new_tree(V, empty(), empty()).

t1() -> bt(1, bt(2, empty(), leaf(3)), leaf(4)).
t2() -> bt(1, bt(5, empty(), leaf(3)), leaf(4)).
t3() -> bt(1, bt(2, leaf(5), leaf(3)), leaf(4)).
t4() -> bt(1, leaf(2),                 leaf(4)).
t5() -> bt(1, bt(2, empty(), leaf(3)), bt(6, leaf(7), leaf(8))).
t6() -> bt(1, bt(2, empty(), leaf(5)), leaf(4)).

data_is_retained_test() ->
Exp = t1(),
?assertMatch(Exp, zipper:to_tree(zipper:from_tree(t1()))).

left_right_and_value_test() ->
?assertMatch(3, zipper:value(zipper:right(zipper:left(zipper:from_tree(t1()))))).

dead_end_test() ->
Exp = empty(),
?assertMatch(Exp, zipper:left(zipper:left(zipper:from_tree(t1())))).

tree_from_deep_focus_test() ->
Exp = t1(),
Zipper = zipper:right(zipper:left(zipper:from_tree(t1()))),
?assertMatch(Exp, zipper:to_tree(Zipper)).

traversing_up_from_top_test() ->
Exp = empty(),
?assertMatch(Exp, zipper:up(zipper:from_tree(t1()))).

left_right_and_up_test() ->
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:up(Act1),
Act3 = zipper:right(Act2),
Act4 = zipper:up(Act3),
Act5 = zipper:left(Act4),
Act6 = zipper:right(Act5),
?assertMatch(3, zipper:value(Act6)).

set_value_test() ->
Exp  = t2(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:set_value(Act1, 5),
Act3 = zipper:to_tree(Act2),
?assertMatch(Exp, Act3).

set_value_after_traversiing_up_test() ->
Exp  = t2(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:right(Act1),
Act3 = zipper:up(Act2),
Act4 = zipper:set_value(Act3, 5),
Act5 = zipper:to_tree(Act4),
?assertMatch(Exp, Act5).

set_left_with_leaf_test() ->
Exp  = t3(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:set_left(Act1, leaf(5)),
Act3 = zipper:to_tree(Act2),
?assertMatch(Exp, Act3).

set_right_with_empty_test() ->
Exp  = t4(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:set_right(Act1, empty()),
Act3 = zipper:to_tree(Act2),
?assertMatch(Exp, Act3).

set_right_with_subtree_test() ->
Exp  = t5(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:set_right(Act0, bt(6, leaf(7), leaf(8))),
Act2 = zipper:to_tree(Act1),
?assertMatch(Exp, Act2).

set_value_from_deep_focus_test() ->
Exp  = t6(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:right(Act1),
Act3 = zipper:set_value(Act2, 5),
Act4 = zipper:to_tree(Act3),
?assertMatch(Exp, Act4).

version_test() ->
?assertMatch(1, zipper:test_version()).``````
``````-module(zipper).

-export([new_tree/3, from_tree/1, to_tree/1, up/1, left/1,
right/1, value/1, set_value/2, set_left/2, set_right/2,
test_version/0, prt/1, prz/1]).

-type( value() :: term() ).
-record( tree, { value :: value(), left :: tree(), right :: tree() } ).
-type( tree() :: #tree{} | nil ).
-type( dir() :: left | right | root ).
-record( ztree, { dir = root :: dir(), value :: value(), left :: tree(), right :: tree() } ).
-record( zipper, { prevs :: [#ztree{}], actual :: #ztree{} } ).

-spec(new_tree(value(), tree(), tree()) -> #tree{}).
new_tree(Value, Left, Right) ->
#tree{value=Value, left=Left, right=Right}.

-spec(to_tree(#zipper{}) -> #tree{}).
to_tree(#zipper{prevs=[], actual=Actual}) ->
z2t(Actual);
to_tree(Zipper) ->
to_tree(up(Zipper)).

-spec(from_tree(#tree{}) -> #zipper{}).
from_tree(Tree) ->
#tree{value=Value, left=Left, right=Right} = Tree,
#zipper{prevs=[], actual=#ztree{value=Value, left=Left, right=Right}}.

-spec(value(#zipper{})-> value()).
value(Zipper) ->
Zipper#zipper.actual#ztree.value.

-spec(set_value(#zipper{}, value()) -> #zipper{}).
set_value(Zipper, Value) ->
#zipper{actual=Actual} = Zipper,
NewActual = Actual#ztree{value=Value},
Zipper#zipper{actual = NewActual}.

-spec(set_left(#zipper{}, tree()) -> #zipper{}).
set_left(Zipper, NewLeftTree) ->
#zipper{actual=Actual} = Zipper,
NewActual = Actual#ztree{left=NewLeftTree},
Zipper#zipper{actual = NewActual}.

-spec(set_right(#zipper{}, tree()) -> #zipper{}).
set_right(Zipper, NewRightTree) ->
#zipper{actual=Actual} = Zipper,
NewActual = Actual#ztree{right=NewRightTree},
Zipper#zipper{actual = NewActual}.

-spec(left(#zipper{}) -> #zipper{} | nil).
left(#zipper{actual=Actual}) when Actual#ztree.left == nil -> nil;
left(Zipper) ->
#zipper{prevs=Prevs, actual=Actual} = Zipper,
#zipper{
prevs = [ Actual#ztree{left=nil} | Prevs ],
actual = t2z(Actual#ztree.left, left)
}.

-spec(right(#zipper{}) -> #zipper{} | nil).
right(#zipper{actual=Actual}) when Actual#ztree.right == nil -> nil;
right(Zipper) ->
#zipper{prevs=Prevs, actual=Actual} = Zipper,
#zipper{
prevs = [ Actual#ztree{right=nil} | Prevs ],
actual = t2z(Actual#ztree.right, right)
}.

-spec(up(#zipper{}) -> #zipper{} | nil).
up(#zipper{prevs=[]}) -> nil;
up(#zipper{prevs=[Prev|Prevs], actual=Actual}) when Actual#ztree.dir == left ->
#zipper{
prevs = Prevs,
actual = Prev#ztree{left = z2t(Actual)}
};
up(#zipper{prevs=[Prev|Prevs], actual=Actual}) when Actual#ztree.dir == right ->
#zipper{
prevs = Prevs,
actual = Prev#ztree{right = z2t(Actual)}
}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% helpers  (for debugging, etc.)

% ztree to tree
-spec(z2t(#ztree{}) -> #tree{}).
z2t(ZTree) ->
#ztree{value=V, left=L, right=R} = ZTree,
#tree{value=V, left=L, right=R}.

% tree to ztree
-spec(t2z(#tree{}, dir()) -> #ztree{}).
t2z(Tree, Dir) ->
#tree{value=V, left=L, right=R} = Tree,
#ztree{dir=Dir, value=V, left=L, right=R}.

% print tree
-spec(prt(#tree{}) -> ok).
prt(Tree) ->
prt(Tree, 4).
prt(Tree, Indent) ->
#tree{value=V, left=L, right=R} = Tree,
IndStr = [ 32 || _ <- lists:seq(1,Indent)],
io:format("~p\n",[V]),
case L of
nil -> ok;
L ->
io:format("~sleft: ", [IndStr]),
prt(L, Indent+4)
end,
case R of
nil -> ok;
R ->
io:format("~sright: ", [IndStr]),
prt(R, Indent+4)
end.

% print zipper
-spec(prz(#zipper{}) -> ok).
prz(Zipper) ->
io:format("Prevs:\n", []),
prz(prevs, lists:reverse(Zipper#zipper.prevs)),
io:format("Actual:\n", []),
prz(ztree, Zipper#zipper.actual, 4).

prz(prevs, []) -> ok;
prz(prevs, [P|Prevs]) ->
prz(ztree, P, 4),
io:format("-----------\n", []),
prz(prevs, Prevs).

prz(ztree, ZTree, Indent) ->   % printing a ztree node
IndStr = [ 32 || _ <- lists:seq(1,Indent)],
#ztree{dir=D, value=V, left=L, right=R} = ZTree,
io:format("~p (on ~p)\n", [V, D]),
case L of
nil -> ok;
L ->
io:format("~sleft: ", [IndStr]),
prt(L, Indent+4)
end,
case R of
nil -> ok;
R ->
io:format("~sright: ", [IndStr]),
prt(R, Indent+4)
end.

test_version() -> 1.``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?