I like this definition (which I likely borrowed from somewhere):
let fac n = scanl1 (*) [1..] !! (n-1)
- scanl1 produces a list of repeatedly applying the operation to elements in the list. So
scanl1 (*) [1..n]provides factorials from 2 to n.
scanl1 (*) [1..], then, is the infinite list of factorials. (Values are only generated once they're asked for...)
- And !! is the "get the nth element" operator.
So this code is basically: n factorial is the nth element in the list of all factorials.
It memoizes itself! (I think?)