aboutsummaryrefslogtreecommitdiff
path: root/challenge-325/deadmarshal/erlang/ch2.erl
blob: 491b8c5b46ea862dc2bc2320cd99c57a0e0c2438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-module(ch2).
-export([final_price/1]).

-spec final_price(L) -> R when 
    L :: [T],
    R :: [T],    
    T :: integer().
final_price(Prices) -> 
  T = lists:foldr(fun do_fold/2,{[],[0]},Prices),
  element(1,T).

do_fold(X,{Ans,[Y|Stack]}) when Y > X ->
  do_fold(X,{Ans,Stack});
do_fold(X,{Ans,[Y|Stack]}) ->
  {[X-Y|Ans],[X,Y|Stack]}.