4 ms·
That was . . . really enlightening. Thank you for taking the time to educate me. I've found the Haskell community uncannily welcoming to newbies, and you seem
by Dove 16y ago
That was . . . really enlightening.
Thank you for taking the time to educate me. I've found the Haskell community uncannily welcoming to newbies, and you seem to be continuing the tradition.
- Dove 16y agoTo complete the exercise (in no particular language), Imperative and stateful: var factorialResult = 1; factorial(n) { if(n <= 1) { factorialResult = 1 } else { factorial(n-1) } factorialResult = factorialResult * n } Imperative and functional: factorial(n) = { var lastResult = 1 if(n <= 1) lastResult = 1 else lastResult = factorial(n-1) return n * lastResult } Declarative and stateful: var factorialResult = 1 factorial(n) { if(n > 1) pointlessFunction(multiplyBy(n), factorial(n-1)) } multiplyBy(n) { factorialResult *= n } Declarative and functional: factorial(n) { if(n <= 1) 1 else n * factorial(n-1) } Everyone learns imperative and stateful. Some people also learn declarative and functional. Good programmers tend to do functional when working imperative. And declarative stateful is possible, but just plain silly. No value judgement can be made between imperative and declarative; do what suits the problem. Sometimes you're following a recipe for cookies, and other times you're parsing XML. Functional is better than stateful, but not in a competitive sense -- not like ipods are better than walkmans. More in a pragmatic sense -- more like laws are better than wars . . . when the law will solve your problem. Sometimes you really do need to talk to the world. For everything else, there's functional programming.