| > You'd probably have even more ways to screw up like this with an OO language My example was about imperative languages. You assertion on how many ways one could screw up on OO is irrelevant and besides, you'd really need a good understanding of OO to even try implementing those. I didn't mean to use to imply any of those methods were bad. I have revised my post so people can't miss the point. That is > It is unfortunate when your ability to program functionally is directly proportional to how much of the standard library you have memorised. This makes functional programming much harder to learn. Now, addressing your concerns. > This page is a terrible example. Most of these are not not what a "reasonable programmer" would do. All of the examples except for few particularly egregious ones seems reasonable to me. For example -- one, need n+k patterns but so what
fac 0 = 1
fac (n+1) = (n+1) * fac n
-- two
fac 0 = 1
fac n = n * fac (n-1)
-- three
fac n = foldl (*) 1 [1..n]
-- four
facs = scanl (*) 1 [1..]
fac n = facs !! n
-- five
fac = foldr (*) 1 . enumFromTo 1
-- six
fac n = product [1..n]
Six fairly distinctive ways. I'm sure there's more that's as elegant if not more elegant ways to do the same thing. |
For example, what foldl does, from my understanding, is to recursively apply a function to both a starting value and the first element of a list, and then the same thing again, where the result from the last call becomes the starting value and the rest of the list becomes the list we're working on, up until the list is empty. Not sure if I've explained this well but I've not been into this long.
If you see the second function, that's what it's doing, except it's working just with numbers and not a list. Since foldl works on lists, you need an enumerator, which in this case is just 1 through to n.
The sixth example is great here. Product could easily be an abstraction on something quite similar to three, which itself could very well be an abstraction quite similar to two (using lists, functions to deal with lists and a given function).
Please correct me if I'm wrong at any point here. I'm looking to improve these skills quite a lot so it'd be very, very welcome.