Skip to content

Instantly share code, notes, and snippets.

@peerreynders
Created February 27, 2017 18:15
Show Gist options
  • Save peerreynders/44a421a327cbdcdcda08435dc664051a to your computer and use it in GitHub Desktop.
Save peerreynders/44a421a327cbdcdcda08435dc664051a to your computer and use it in GitHub Desktop.
Functional Programming in Erlang - 2.6 Practice
-module(prac206).
-export([product/1, maximum/1, product1/1, maximum1/1]).
product(Xs) ->
product(Xs,1).
% The product of an empty list is usually taken to be 1: why?
% Identity property of Multiplication:
% Multiplication of any number with 1 results in the original number
product([],P) -> P;
product([X|Xs],P) -> product(Xs,X*P).
product1([]) -> 1;
product1([X|Xs]) -> X * product1(Xs).
maximum([X|Xs]) ->
maximum(Xs,X).
maximum([],Max) -> Max;
maximum([X|Xs],Y) when X > Y -> maximum(Xs,X);
maximum([_|Xs],Max) -> maximum(Xs,Max).
maximum1([Max]) -> Max;
maximum1([X|Xs]) ->
Max = maximum1(Xs),
if
Max > X -> Max;
true -> X
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment