Created
September 27, 2020 21:38
-
-
Save aaronlelevier/3836f5bb70841a58b8b024782ea918b0 to your computer and use it in GitHub Desktop.
Practice with Lists in Erlang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%%------------------------------------------------------------------- | |
%%% @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