I’ve been wanting to learn some Haskell for a while now, and I know it and similar languages have really good support for various kinds of infinite lists. So, how could I represent the sequence of tetrahedral numbers in Haskell, preferably with an 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, the second column is a running total of the first column, and the third column is a running total of the second column. I’d prefer that the Haskell code retain something of the “running total” approach, since that’s the concept I was wondering how to express.
You’re correct, Haskell is really nice for doing things like this:
first_colis an infinite list of integers, starting at 0scanl (+)calculates a lazy running sum: Prelude docsWe can verify that the above code is doing the right thing: