I’ve got a good idea of what primitive recursive definitions are, however I still can’t seem to get my head around it.
For example, I can’t seem to explain to myself how to do the following (yet I seem to be able to do it):
Exercise:
Define the function
productIt :: [Int] -> Int
which gives the product of a list of integers, and returns 1 for an empty list; why is this particular value chosen as the result for the empty list?
I (of course) came up with the solution:
productIt :: [Int] -> Int
productIt [] = 1
productIt (x:xs) = x * productIt xs
which works perfectly for the question in the exercise. Yet I still seem to have trouble getting my head around the final line.
Any ideas on how to think this out would be most appreciated.
In English, you could read that final line as:
Converting it to English like this usually helps me understand how and why a recursive function works. You could also evaluate it with the Haskell interpreter inside your head: