3 ms·
Great explanation, actually. But one question. I thought functional languages were "pure" throughout. I mean, I thought the whole point of a functional language
by mitchellh 15y ago
Great explanation, actually. But one question. I thought functional languages were "pure" throughout. I mean, I thought the whole point of a functional language is that there are no side effects and every expression returns some value. How is "pure" distinct from "functional"?
Oh, no no. Pure is not required for functional programming. Functional programming is mostly defined as the ability to pass around functions as 1st class data types. In fact, I noticed on your GitHub you do some Python. Here is functional programming in Python:
# Sum a list in Python using FP
items = [1,2,3,4,5]
function = lambda x, y: x + y
reduce(function, items) # => 15
# The equivalent in haskell:
foldr1 (+) [1,2,3,4,5]
Pure is a stricter form of functional programming, which Haskell enforces.
edit: BTW, Haskell DOES have a steep learning curve, and the inherent recursion breaks my brain. Even simple things like:
`combos = []` `combos = combos + 'foo'`
hurt, because counterintuitively (to me, anyway) this winds up in infinite recursion. I'm very used to OOP so this sort of thing is hard on me. I'm old though.
You're stepping now into the "lazy" aspect of Haskell. You're allowed to have infinite recursion in Haskell because it is only evaluated when needed. I don't really want to get into it here since there are many resources online about it :) But this also isn't inherent to functional programming or pure function programming.
- mattdeboard 15y agoInteresting, thanks for taking the time for in-depth explanations.
- eru 15y agoAlso you don't need to use recursion too much, when writing Haskell code. You should stick to using combinators over your (recursive) data-structures. Think fold, map, filter, and friends. Alas, you still have to be able to understand recursion to get anywhere.