🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of maxmuen

maxmuen's solution

to Zipper in the Erlang Track

Published at Mar 08 2020 · 0 comments
Instructions
Test suite
Solution

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

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

-record(tree, {value, left, right }).
-record(zipper, {node, path = []}).

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

to_tree(#zipper{node=Node, path=[]}) -> Node;
to_tree(Zipper) -> to_tree(up(Zipper)).

from_tree(Tree) -> (#zipper{node=Tree}).

value(#zipper{node=#tree{value=Value}}) -> Value.

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

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

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

left(#zipper{node=Node}) when Node#tree.left == nil -> nil;
left(#zipper{node=Node, path=Path}) -> #zipper{node=Node#tree.left, path=[{left, Node}|Path]}.

right(#zipper{node=Node}) when Node#tree.right == nil -> nil;
right(#zipper{node=Node, path=Path}) -> #zipper{node=Node#tree.right, path=[{right, Node}|Path]}.

up(#zipper{path=[]}) -> nil;
up(#zipper{node=Node, path=[{left, Parent}|Path]}) ->
  #zipper{node=Parent#tree{left = Node}, path=Path};
up(#zipper{node=Node, path=[{right, Parent}|Path]}) ->
  #zipper{node=Parent#tree{right = Node}, path=Path}.

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?