Skip to content

Instantly share code, notes, and snippets.

@gangleri
Created February 22, 2017 10:58
Show Gist options
  • Save gangleri/60c063f0a8ef2555b469efcc26312aa3 to your computer and use it in GitHub Desktop.
Save gangleri/60c063f0a8ef2555b469efcc26312aa3 to your computer and use it in GitHub Desktop.
Functional Programming in Erlang Week 1
-module(shapes).
-export([perimeter/1, area/1, enclose/1]).
-export([test/0]).
perimeter({rectangle, L, B}) ->
L * 2 + B * 2;
perimeter({circle, R}) ->
2 * math:pi() * R;
perimeter({triangle, A, B, C}) ->
A + B + C.
area({rectangle, W, H}) ->
W * H;
area({circle, R}) ->
math:pi() * math:pow(R, 2);
area(T = {triangle, A, B, C}) ->
S = perimeter(T) / 2,
math:sqrt(S * (S - A) * (S - B) * (S - C)).
height(T = {triangle, A, B, C}) ->
2 * area(T) / max(max(A, B), C).
enclose(R = {rectangle, _, _}) ->
R;
enclose({circle, R}) ->
{rectangle, 2*R, 2*R};
enclose(T = {triangle, A, B, C}) ->
{rectangle, max(max(A, B), C), height(T)}.
test() ->
Rectangle = {rectangle, 5, 4},
Triangle = {triangle, 6, 7, 8},
Circle = {circle, 6},
18 = perimeter(Rectangle),
21 = perimeter(Triangle),
37.69911184307752 = perimeter(Circle),
20 = area(Rectangle),
20.33316256758894 = area(Triangle),
113.09733552923255 = area(Circle),
{rectangle, 5, 4} = enclose(Rectangle),
{rectangle, 8, 5.083290641897235} = enclose(Triangle),
{rectangle, 12, 12} = enclose(Circle),
success.
-module(sumbits).
-export([bits/1, bitsT/1, test/0]).
% direct recursion
bits(0) -> 0;
bits(N) when N > 0 ->
bits(N div 2) + N rem 2.
% tail recursion
% With tail recursion you only bring what you
% need onto the next call, which minimizes memory
% uses on the stack. Also, when the tail recursive
% code is optimized, function returns that are not
% needed are thrown away which will make it slightly
% faster in some cases. This is therefor my preferred
% implementation
bitsT(N) when N > 0 -> bitsT(N,0).
bitsT(0,Total) -> Total;
bitsT(N,Total) -> bitsT(N div 2, Total + N rem 2).
% this implementation converts the number to binary
% form and recurse through the list, it has the exra
% overhead of converting a string to a number that the
% previous implementations don't have
bitsL(N) when is_integer(N) andalso N >=0 ->
bitsL(integer_to_list(N, 2), 0).
bitsL([], Total) ->
Total;
bitsL([H|T], Total) ->
{X, _} = string:to_integer([H]),
bitsL(T, Total+X).
test() ->
1 = bits(8),
1 = bitsT(8),
1 = bitsL(8),
3 = bits(7),
3 = bitsT(7),
3 = bitsL(7),
2 = bits(3),
2 = bitsT(3),
2 = bitsL(3),
success.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment