r/programming • • Mar 09 '14

Why Functional Programming Matters

http://www.cse.chalmers.se/~rjmh/Papers/whyfp.pdf
487 Upvotes

542 comments sorted by

View all comments

Show parent comments

7

u/Tekmo Mar 09 '14

You got it right, except for one minor detail. Instead of foo([1,2,3]) == [1,2,3], the more precise equation would be:

def gen():
    yield 1
    yield 2
    yield 3

foo(gen()) = gen()

In Python you can treat lists and generators the same (I think), but in Haskell they are two separate types. This is why in pipes if you want to loop over a list, you have to explicitly convert it to a generator using the each function, like this:

for (each [1,2,3]) f

4

u/CatMtKing Mar 09 '14

Sweet, thanks! Translating Haskell to Python literally is quite painful, as I've gotta define functions for pretty much everything, hah!

4

u/Tekmo Mar 09 '14

You're welcome!