Skip to content

Instantly share code, notes, and snippets.

@rwngallego
Created March 10, 2017 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwngallego/a27d9c2db824390c6de0b3ece7fd4a66 to your computer and use it in GitHub Desktop.
Save rwngallego/a27d9c2db824390c6de0b3ece7fd4a66 to your computer and use it in GitHub Desktop.
-module(lists_module).
-export([product/1, maximum/1, product_t/1, maximum_t/1]).
% direct
product([]) ->
1;
product([H|T]) ->
H*product(T).
maximum([H|[]]) ->
H;
maximum([H|T]) ->
max(H, maximum(T)).
% tail
product_t([_|_]=L) -> product_t(L, 1).
product_t([], N) -> N;
product_t([H|T], N) -> product_t(T, N*H).
maximum_t([_|_]=L) ->
maximum_t(L, 0).
maximum_t([], N) ->
N;
maximum_t([H|T], N) ->
maximum_t(T, max(H, N)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment