Skip to content

Instantly share code, notes, and snippets.

@jamesduncombe
Last active August 29, 2015 14:01
Show Gist options
  • Save jamesduncombe/11c2090d2d291cb1c377 to your computer and use it in GitHub Desktop.
Save jamesduncombe/11c2090d2d291cb1c377 to your computer and use it in GitHub Desktop.
Playing with Erlang
#!/usr/bin/env escript
main(_) ->
List = [1,2,3,4,5,6,7,8,9],
NewList = reverse(List),
io:format(NewList),
shifter("This should not pass correctly"),
%% list comprehension, because they are cool in Erlang
Comprehended = [ N bsl 4 || N <- List, N =/= 1 ],
io:format("~p~n", [Comprehended]),
%% ..and again, because we can, anonymous functions...
Blah = fun() ->
io:format("I am inside~n")
end,
Blah(),
%% Erlang has some interesting integer handling too...
io:format("~p~n", [2#1010]), %% should be 10 (base 2)
%% currying...
D = fun(X) ->
fun(Y) ->
X div Y
end
end,
D10div = D(10),
Total = D10div(20),
io:format("~p~n", [Total]).
%% reverser thing using tail recursion
reverse(L) -> reverse(L,[]).
reverse([], Acc) -> Acc;
reverse([H|T], Acc) ->
%% testing string interpolation / formatting
io:format("Tail: ~p, Acc: ~p~n", [T,Acc]),
%% testing out if statements
if
H =:= 2 -> io:format("Up at 2 yo!~n");
true -> ok
end,
reverse(T, [shifter(H)|Acc]).
%% testing out guard clauses
shifter(N) when is_integer(N) ->
N bsl 2;
shifter(_) ->
io:format("Throw me a bone yo!").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment