Skip to content

Instantly share code, notes, and snippets.

@aaronlelevier
Created September 27, 2020 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronlelevier/3836f5bb70841a58b8b024782ea918b0 to your computer and use it in GitHub Desktop.
Save aaronlelevier/3836f5bb70841a58b8b024782ea918b0 to your computer and use it in GitHub Desktop.
Practice with Lists in Erlang
%%%-------------------------------------------------------------------
%%% @author Aaron Lelevier
%%% @doc Practice with Lists in Erlang. It's been about a week since coding up some Erlang
%%% These functions are all in the [lists](http://erlang.org/doc/man/lists.html) module
%%% @end
%%% Created : 27. Sep 2020 2:26 PM
%%%-------------------------------------------------------------------
-module(practice).
-author("Aaron Lelevier").
-vsn(1.0).
-export([seq/2, zip/2, unzip/1]).
-compile(export_all).
seq(X, Y) ->
seq(X, Y, []).
seq(X, Y, Acc) ->
if
X =< Y ->
seq(X + 1, Y, [X | Acc]);
true ->
lists:reverse(Acc)
end.
zip(L1, L2) ->
zip(L1, L2, []).
zip([], [], Acc) -> lists:reverse(Acc);
zip([H1|T1], [H2|T2], Acc) ->
zip(T1, T2, [{H1, H2}|Acc]).
unzip(L) ->
unzip(L, [], []).
unzip([], Acc1, Acc2) -> {lists:reverse(Acc1), lists:reverse(Acc2)};
unzip([{A,B}|T], Acc1, Acc2) ->
unzip(T, [A|Acc1], [B|Acc2]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment