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_col infinite list of integers, starting @ 0
  • scanl (+) 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

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -