Last active
January 11, 2017 14:05
-
-
Save cystbear/b27bc5754ffa11fec634 to your computer and use it in GitHub Desktop.
Academic problems solved with Erlang
This file contains hidden or 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
%% https://en.wikipedia.org/wiki/Collatz_conjecture | |
-module(collatz). | |
-export([collatz/1]). | |
collatz(N) -> collatz(N,0,[N]). | |
collatz(1, C, Acc) -> {C, lists:reverse(Acc)}; | |
collatz(N, C, Acc) -> | |
R = case is_even(N) of | |
true -> trunc(N/2); | |
false -> trunc(N*3 + 1) | |
end, | |
collatz(R, C+1, [R|Acc]). | |
is_even(I) -> I rem 2 =:= 0. |
This file contains hidden or 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
-module(compose). | |
-export([compose/0, compose/1]). | |
compose() -> compose([]). | |
compose([]) -> fun(X) -> X end; | |
compose(L) -> fun(X) -> lists:foldr(fun(F, V) -> F(V) end, X, L) end. |
This file contains hidden or 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
%% https://en.wikipedia.org/wiki/Currying | |
-module(curry). | |
-export([curry_simpl/1, curry/1]). | |
curry_simpl(F) -> curry_simpl(F, []). | |
curry_simpl(F, Args) -> | |
{arity,Arity} = erlang:fun_info(F, arity), | |
case length(Args) == Arity of | |
true -> apply(F, Args); | |
false -> fun (Arg) -> curry_simpl(F, Args ++ [Arg]) end | |
end. | |
curry(F) when is_function(F) -> curry(F, []). | |
curry(F, Args) when is_function(F), is_list(Args)-> | |
{arity,Arity} = erlang:fun_info(F, arity), | |
case length(Args) >= Arity of | |
true -> apply(F, lists:sublist(Args, Arity)); | |
false -> fun(Arg) -> curry(F, lists:flatten([Args, Arg])) end | |
end. |
This file contains hidden or 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
%% https://en.wikipedia.org/wiki/Fibonacci_number | |
-module(fib). | |
-export([fib/1]). | |
fib(N) when N > 0 -> fib(N, 1, 0, 1). | |
fib(1, _, _, _) -> 1; | |
fib(2, _, _, _) -> 1; | |
fib(N, N, _, Prev) -> Prev; | |
fib(N, C, Prev2, Prev) -> fib(N, C+1, Prev, Prev2+Prev). |
This file contains hidden or 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
-module(is_proplist). | |
-export([is_proplist/1]). | |
is_proplist(L) when is_list(L) -> | |
lists:all(fun | |
({K,_}) when is_atom(K) -> true; | |
({K,_}) when is_binary(K) -> io_lib:printable_latin1_list(binary_to_list(K)); | |
(_) -> false | |
end, L); | |
is_proplist(_) -> false. |
This file contains hidden or 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
-module(lazy). | |
-compile(export_all). | |
%% generator(F, V) -> fun() -> [ V | generator(F, F(V)) ] end. | |
generator(F, V) -> [V | fun() -> generator(F, F(V)) end]. | |
take(N, G) when N >= 0 -> lists:reverse(take(N, G, 0, [])). | |
take(_,[],_,Acc) -> Acc; | |
take(N,_,N,Acc) -> Acc; | |
take(N, [H|T], C, Acc) when is_function(T) -> take(N, T(), C+1, [H|Acc]); | |
take(N, [H|T], C, Acc) -> take(N, T, C+1, [H|Acc]). |
This file contains hidden or 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
%% Take N first elements of list | |
-module(take). | |
-export([take/2]). | |
take(N, L) when N > 0 -> lists:reverse(take(N, L, 0, [])). | |
take(_,[],_,Acc) -> Acc; | |
take(N,_,N,Acc) -> Acc; | |
take(N, [H|T], C, Acc) -> take(N, T, C+1, [H|Acc]). |
This file contains hidden or 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
%% http://learnyouahaskell.com/higher-order-functions#higher-orderism | |
-module(zip_with). | |
-export([zip_with/3]). | |
zip_with(F, L1, L2) -> | |
lists:reverse(zip_with(F, L1, L2, [])). | |
zip_with(_,[],_,Acc) -> Acc; | |
zip_with(_,_,[],Acc) -> Acc; | |
zip_with(F, [H1|T1], [H2|T2], Acc) -> zip_with(F, T1, T2, [F(H1,H2) | Acc]). |
Just added compose.erl
file. Function Composing implementation.
https://gist.github.com/cystbear/b27bc5754ffa11fec634#file-compose-erl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just added
curry.erl
with Erlang currying inside, do not miss it!https://gist.github.com/cystbear/b27bc5754ffa11fec634#file-curry-erl