Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(step5).
- -export([sum/1, sumT/1, product/1, productT/1, max/1, maxT/1]).
- sum ([]) ->
- 0;
- sum ([X|Xs]) ->
- X + sum(Xs).
- sumT (Xs) ->
- % 1. initialization
- sumT(Xs, 0).
- % 2. test/condition part/step (if something then return ...)
- sumT ([], S) ->
- % 3. execution step
- S;
- % 2. test/condition part/step (if something other then return ...)
- sumT ([X|Xs], S) ->
- % ^
- % |
- % 4. incrementation part/step
- % 3. execution step
- sumT(Xs, X + S).
- % My reason why we mutliple by 1:
- % it needs to be one because multiplying something by zero gives zero
- % and we multiply our accumulator by the product of an empty list
- %
- % Reason after reading others comments
- % Multiplying by one does not change value of multiplied element :)
- product ([]) ->
- 1;
- product ([X|Xs]) ->
- X * product(Xs).
- productT (Xs) ->
- productT(Xs, 1).
- productT ([], A) ->
- A;
- productT ([X|Xs], A) ->
- productT(Xs, A * X).
- max([X]) ->
- X;
- max([X|Xs]) ->
- max(X, max(Xs)).
- maxT([X|Xs]) ->
- maxT(Xs, X).
- maxT([X], MaxValue) ->
- max(X, MaxValue);
- maxT([X|Xs], MaxValue) ->
- maxT(Xs, max(X, MaxValue)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement