Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of evnu

evnu's solution

to Zipper in the Erlang Track

Published at Apr 11 2020 · 0 comments
Test suite

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


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.




%% 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).


-record(tree, {value, left, right}).
-record(zipper, {parent, tree}).

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

to_tree(#zipper{parent=nil, tree=Tree}) ->
to_tree(#zipper{parent=Parent, tree=Tree}) ->
    to_tree(Parent#zipper{tree=merge(Parent#zipper.tree, Tree)}).

merge(Tree = #tree{left=hole}, Cut) ->
merge(Tree = #tree{right=hole}, Cut) ->

from_tree(Tree) ->
    #zipper{parent=nil, tree=Tree}.

value(Zipper) ->
    Tree = Zipper#zipper.tree,

set_value(Zipper, Value) ->
    Tree = Zipper#zipper.tree,
    NewTree = Tree#tree{value=Value},

set_left(Zipper, Value) ->
    Tree = Zipper#zipper.tree,
    NewTree = Tree#tree{left=Value},

set_right(Zipper, Value) ->
    Tree = Zipper#zipper.tree,
    NewTree = Tree#tree{right=Value},

left(Zipper) ->
    Tree = Zipper#zipper.tree,
    case Tree#tree.left of
	nil -> nil;
	Left -> #zipper{parent=cut(Zipper, left), tree=Left}

right(Zipper) ->
    Tree = Zipper#zipper.tree,
    case Tree#tree.right of
	nil -> nil;
	Right -> #zipper{parent=cut(Zipper, right), tree=Right}

cut(Zipper, left) ->
    Tree = Zipper#zipper.tree,
    NewTree = Tree#tree{left=hole},
cut(Zipper, right) ->
    Tree = Zipper#zipper.tree,
    NewTree = Tree#tree{right=hole},

up(#zipper{parent=nil}) ->
up(#zipper{parent=Parent, tree=Tree}) ->
    ParentTree = Parent#zipper.tree,
    Parent#zipper{tree=merge(ParentTree, Tree)}.

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?