Last active
May 23, 2020 03:16
-
-
Save stormwatch/d0c672cfd7935c3e3b35d4e04acf4e86 to your computer and use it in GitHub Desktop.
2.15 Defining functions over lists in practice, https://www.futurelearn.com/courses/functional-programming-erlang/3/steps/488110
This file contains hidden or 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(two15). | |
-export([product/1, product_tr/1, maximum/1, maximum_tr/1]). | |
product([X]) -> | |
X; | |
product([X|Xs]) -> | |
X * product(Xs). | |
product_tr([X|Xs]) -> | |
product_tr(Xs, X). | |
product_tr([], Result) -> | |
Result; | |
product_tr([X|Xs], Result_so_far) -> | |
product_tr(Xs, X * Result_so_far). | |
maximum([X]) -> | |
X; | |
maximum([X, Y | Zs]) -> | |
maximum([max(X, Y) | Zs]). | |
maximum_tr([X|Xs]) -> | |
maximum_tr(Xs, X). | |
maximum_tr([], Result) -> | |
Result; | |
maximum_tr([X|Xs], Result_so_far) when X > Result_so_far -> | |
maximum_tr(Xs, X); | |
maximum_tr([_X|Xs], Result_so_far) -> | |
maximum_tr(Xs, Result_so_far). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment