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

shandaman's solution

to List Ops in the Erlang Track

Published at Dec 22 2020 · 0 comments
Instructions
Test suite
Solution

Implement basic list operations.

In functional languages list operations like length, map, and reduce are very common. Implement a series of basic list operations, without using existing functions.

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.

list_ops_tests.erl

%% based on canonical data version 2.3.0
%% https://raw.githubusercontent.com/exercism/problem-specifications/master/exercises/list-ops/canonical-data.json

-module(list_ops_tests).

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

append_empty_lists_test() ->
	?assertEqual([],
		     list_ops:append([], [])).

append_empty_list_to_list_test() ->
	?assertEqual([1,2,3,4],
		     list_ops:append([], [1,2,3,4])).

append_non_empty_lists_test() ->
	?assertEqual([1,2,2,3,4,5],
		     list_ops:append([1,2], [2,3,4,5])).

concat_empty_list_test() ->
	?assertEqual([],
		    list_ops:concat([])).

concat_list_of_lists_test() ->
	?assertEqual([1,2,3,4,5,6],
		     list_ops:concat([[1,2], [3], [], [4,5,6]])).

concat_list_of_nested_lists_test() ->
	?assertEqual([[1], [2], [3], [], [4,5,6]],
		     list_ops:concat([[[1], [2]], [[3]], [[]], [[4,5,6]]])).

filter_empty_list_test() ->
	?assertEqual([],
		     list_ops:filter(fun(X) -> X rem 2 =:= 1 end, [])).

filter_non_empty_list_test() ->
	?assertEqual([1,3,5],
		     list_ops:filter(fun(X) -> X rem 2 =:= 1 end, [1,2,3,5])).

length_empty_list_test() ->
	?assertEqual(0,
		     list_ops:length([])).

length_non_empty_list_test() ->
	?assertEqual(4,
		     list_ops:length([1,2,3,4])).

map_empty_list_test() ->
	?assertEqual([],
		     list_ops:map(fun(X) -> X + 1 end, [])).

map_non_empty_list_test() ->
	?assertEqual([2,4,6,8],
		     list_ops:map(fun(X) -> X + 1 end, [1,3,5,7])).

foldl_empty_list_test() ->
	?assertEqual(2,
		     list_ops:foldl(fun(X,Y) -> X * Y end, 2, [])).

foldl_direction_independent_function_applied_to_non_empty_list_test() ->
	?assertEqual(15,
		     list_ops:foldl(fun(X,Y) -> X + Y end, 5, [1,2,3,4])).

foldl_direction_dependent_function_applied_to_non_empty_list_test() ->
	?assertEqual(0,
		     list_ops:foldl(fun(X,Y) -> Y div X end, 5, [2,5])).

foldr_empty_list_test() ->
	?assertEqual(2,
		     list_ops:foldr(fun(X,Y) -> X * Y end, 2, [])).

foldr_direction_independent_function_applied_to_non_empty_list_test() ->
	?assertEqual(15,
		     list_ops:foldr(fun(X,Y) -> X + Y end, 5, [1,2,3,4])).

foldr_direction_dependent_function_applied_to_non_empty_list_test() ->
	?assertEqual(2,
		     list_ops:foldr(fun(X,Y) -> X div Y end, 5, [2,5])).

reverse_empty_list_test() ->
	?assertEqual([],
		     list_ops:reverse([])).

reverse_non_empty_list_test() ->
	?assertEqual([7,5,3,1],
		     list_ops:reverse([1,3,5,7])).
-module(list_ops).

-export([append/2, concat/1, filter/2, length/1, map/2, foldl/3, foldr/3, reverse/1]).

append(List1, List2) ->
    append(List1, List2, []).

append([], [], Acc) ->
    reverse(Acc);
append([], [Head | Tail], Acc) ->
    append([], Tail, [Head | Acc]);
append([Head | Tail], List2, Acc) ->
    append(Tail, List2, [Head | Acc]).

concat(List) ->
    concat(List, []).

concat([], Acc) ->
    Acc;
concat([Head | Tail], Acc) ->
    concat(Tail, append(Acc, Head)).

filter(Function, List) ->
    filter(Function, List, []).

filter(_Function, [], Acc) ->
    reverse(Acc);
filter(Function, [Head | Tail], Acc) ->
    case Function(Head) of
        true ->
            filter(Function, Tail, [Head | Acc]);
        false ->
            filter(Function, Tail, Acc)
    end.

length(List) ->
    length(List, 0).

length([], Sum) ->
    Sum;
length([_Head | Tail], Sum) ->
    length(Tail, Sum + 1).

map(Function, List) ->
    map(Function, List, []).

map(_Function, [], Acc) ->
    reverse(Acc);
map(Function, [Head | Tail], Acc) ->
    map(Function, Tail, [Function(Head) | Acc]).

foldl(_Function, Start, []) ->
    Start;
foldl(Function, Start, [Head | Tail]) ->
    foldl(Function, Function(Head, Start), Tail).

foldr(Function, Start, List) ->
    foldl(Function, Start, reverse(List)).

reverse(List) ->
    reverse(List, []).

reverse([], Acc) ->
    Acc;
reverse([Head | Tail], Acc) ->
    reverse(Tail, [Head | Acc]).

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?