list - Represent sequence of tetrahedral numbers in Haskell -
i've been wanting learn haskell while now, , know , similar languages have support various kinds of infinite lists. so, how represent sequence of tetrahedral numbers in haskell, preferably explanation of what's going on?
0   0   0 1   1   1 2   3   4 3   6   10 4   10  20 5   15  35 6   21  56 7   28  84 8   36  120   in case it's not clear what's going on there, second column running total of first column, , third column running total of second column. i'd prefer haskell code retain of "running total" approach, since that's concept wondering how express.
you're correct, haskell nice doing things this:
first_col = [0..] second_col = scanl1 (+) first_col third_col = scanl1 (+) second_col   first_colinfinite list of integers, starting @ 0scanl (+)calculates lazy running sum: prelude docs
we can verify above code doing right thing:
prelude> take 10 first_col  [0,1,2,3,4,5,6,7,8,9] prelude> take 10 second_col  [0,1,3,6,10,15,21,28,36,45] prelude> take 10 third_col  [0,1,4,10,20,35,56,84,120,165]      
Comments
Post a Comment