/* Common utils in Picat. Utilities that are used both in ppl_utils.pi and ppl_distributions. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ module ppl_common_utils. import util. /* cumsum(L) Accumulative sum of L */ cumsum(L) = scanl1(+,L). asum(L) = cumsum(L). % alias scanl(_F, E, []) = [E]. scanl(F,E,Xs) = [E] ++ scanlT(F,E,Xs). scanlT(_F,_A,[]) = []. scanlT(F,A,[Y|Ys]) = scanl(F, apply(F,A,Y), Ys). scanl1(_F,[]) = _ => throw $error(empty_list,scanl1,[]). scanl1(F, [X|Xs]) = scanl(F,X,Xs). /* differences(L) Returns the differences of L */ differences(L) = [L[I+1]-L[I] : I in 1..L.len-1].