Created
March 11, 2017 19:04
-
-
Save smarj/ff0bf6e19ae66f378cfdfc5da9bdd541 to your computer and use it in GitHub Desktop.
Functional Programming in Erlange 2.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(w2a1). | |
-export([maximum/1, | |
product/1, | |
tail_maximum/1, | |
tail_product/1]). | |
product([]) -> 1; | |
product([Num|Nums]) -> Num * product(Nums). | |
tail_product(Nums) -> tail_product(Nums, 1). | |
tail_product([], Acc) -> Acc; | |
tail_product([Num|Nums], Acc) -> | |
tail_product(Nums, Num * Acc). | |
maximum([X|[]]) -> X; | |
maximum([X|Xs]) -> | |
max(X, maximum(Xs)). | |
tail_maximum([X|[]]) -> X; | |
tail_maximum([X|Xs]) -> tail_maximum(Xs, X). | |
tail_maximum([X|[]], Acc) -> max(X, Acc); | |
tail_maximum([X|Xs], Acc) -> | |
tail_maximum(Xs, max(X, Acc)). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment