Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. -module(functions_over_lists).
  2.  
  3. -export([product/1]).
  4. -export([maximum/1]).
  5. -export([product_fold/1]).
  6.  
  7. product(X) -> product(X, 1).
  8. product([], Product) -> Product;
  9. product([X | Xs], Product) -> product(Xs, X * Product).
  10.  
  11. product_fold(Xs) ->
  12. lists:foldl(fun(X, P) -> X * P end, 1, Xs).
  13.  
  14. % Return 'none' for empty lists
  15. maximum([]) -> none;
  16. % Initialize Max to the first list item
  17. maximum([X | Xs]) -> maximum(Xs, X).
  18. % When all items are exhausted, return held Max
  19. maximum([], Max) -> Max;
  20. % Replace Max when a list item is a number and larger than the current Max
  21. maximum([X | Xs], Max) when is_number(X), X > Max -> maximum(Xs, X);
  22. % Otherwise continue on to the next item
  23. maximum([_ | Xs], Max) -> maximum(Xs, Max).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement