Skip to content

Instantly share code, notes, and snippets.

@cystbear
Last active January 11, 2017 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cystbear/b27bc5754ffa11fec634 to your computer and use it in GitHub Desktop.
Save cystbear/b27bc5754ffa11fec634 to your computer and use it in GitHub Desktop.
Academic problems solved with Erlang
%% 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.
-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.
%% 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.
%% 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).
-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.
-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]).
%% 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]).
%% 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]).
@cystbear
Copy link
Author

my Erlang snippets storage

@cystbear
Copy link
Author

Just added curry.erl with Erlang currying inside, do not miss it!
https://gist.github.com/cystbear/b27bc5754ffa11fec634#file-curry-erl

@cystbear
Copy link
Author

cystbear commented Nov 7, 2015

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