Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Functional Programming without recursion?

Name: Anonymous 2011-06-04 23:39

I'm really interested in functional programming and I've been learning about it for a while a quite a while now but I've hit a subject which has left me stumped. That is recursion. I mean fucking hell, I can't figure it out! When I was learning how to code I was seeing nothing but for loops in other peoples code so that's how I do things. For loops everywhere! Hell I barely know how to use a while loop. Can someone tell me if there's a point to functional programming without recursion? Can you use iteration? Maybe you can explain recursion or link me to good examples because I sure haven't come across any.

Name: Anonymous 2011-06-05 1:49

Here's a factorial example in Haskell:

fact 0 = 1 -- this is the base case - when you reach this, you know you're done
fact n = n * fact (n - 1) -- general rule - apply it until you reach the base case


Here's how this is evaluated:

fact 4
4 * fact 3
4 * (3 * fact 2)
4 * (3 * (2 * fact 1))
4 * (3 * (2 * (1 * fact 0)))
4 * (3 * (2 * (1 * 1)))
4 * (3 * (2 * 1))
4 * (3 * 2)
4 * 6
24


Also, you should try to use higher-order functions like map, filter and fold* instead of recursion whenever possible.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List