Skip to content

Instantly share code, notes, and snippets.

@bussiere
Created November 20, 2010 16:57
Show Gist options
  • Save bussiere/707956 to your computer and use it in GitHub Desktop.
Save bussiere/707956 to your computer and use it in GitHub Desktop.
my tests in erlang
-module(test).
-export([greet/2,head/1,second/1,same/2,right_age/1,oh_god/1,help_me/1,prepend/2,beach/1,beachf/1,convert/0]).
greet(male,Name)->
io:format("Hello, Mr. ~s!",[Name]);
greet(female,Name) ->
io:format("Hello, Mrs. ~s!",[Name]);
greet(_,Name) ->
io:format("Hello, ~s!",[Name]).
head([H|_]) -> H.
second([_,H|_]) ->H.
same(X,X) ->
true;
same(_,_) ->
false.
right_age(X) when X >= 16, X =< 104 ->
true;
right_age(_) ->
false.
oh_god(N) ->
if N =:= 2 -> might_succeed;
true -> always_does %% this is Erlang's if's 'else!'
end.
help_me(Animal) ->
Talk = if Animal == cat -> "meow";
Animal == beef -> "mooo";
Animal == dog -> "bark";
Animal == tree -> "bark";
true -> "fgdadfgna"
end,
{Animal, "says " ++ Talk ++ "!"}.
prepend(X,[]) ->
[X];
prepend(X,Set) ->
case lists:member(X,Set) of
true -> Set;
false -> [X|Set]
end.
beach(Temperature) ->
case Temperature of
{celsius, N} when N >= 20, N =< 45 ->
'favorable';
{kelvin, N} when N >= 293, N =< 318 ->
'scientifically favorable';
{fahrenheit, N} when N >= 68, N =< 113 ->
'favorable in the US';
_ ->
'avoid beach'
end.
beachf({celsius, N}) when N >= 20, N =< 45 ->
'favorable';
beachf({kelvin, N}) when N >= 293, N =< 318 ->
'scientifically favorable';
beachf({farenheit,N}) when N >= 68, N =< 113 ->
'favorable in the US';
beachf(_) ->
'avoid beach'.
convert()->
erlang:list_to_integer("54"),
erlang:integer_to_list(54),
erlang:list_to_float("54.32"),
erlang:atom_to_list(true),
erlang:list_to_bitstring("hi there"),
erlang:bitstring_to_list(<<"hi there">>).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment