Permalink
Browse files

erl learning

  • Loading branch information...
1 parent d8f18ee commit 7866a4c3d1d110df354ff2e78a150a3da8c2e75a @rskokan committed Oct 2, 2011
Showing with 41 additions and 0 deletions.
  1. +41 −0 my_module.erl
View
@@ -0,0 +1,41 @@
+%% This is a simple Erlang module
+-module(my_module).
+-export([pie/0, print/1, printw/1, either_or_both/2, either_or_both2/2, area/1]).
+
+pie() ->
+ 3.14.
+
+print(Term) ->
+ io:format("The value of Term is ~p.~n", [Term]).
+
+printw(Term) ->
+ io:format("The value of Term is ~w.~n", [Term]).
+
+either_or_both(true, B) when is_boolean(B) ->
+ true;
+either_or_both(A, true) when is_boolean(A) ->
+ true;
+either_or_both(false, false) ->
+ false;
+either_or_both(_, _) ->
+ %% Just to try to catch this case and print something. But better to raise an error.
+ io:format("ERROR: Arguments must be booleans~n").
+
+%% Same using case (not much Erlang-ish)
+either_or_both2(A, B) ->
+ case {A, B} of
+ {true, B} when is_boolean(B) ->
+ true;
+ {A, true} when is_boolean(A) ->
+ true;
+ {false, false} ->
+ false
+ end.
+
+area({circle, Radius}) ->
+ Radius * Radius * math:pi();
+area({square, Side}) ->
+ Side * Side;
+area({rectangle, Width, Height}) ->
+ Width * Height.
+

0 comments on commit 7866a4c

Please sign in to comment.