Live data from Hacker News

Notes on Haskell: What's Wrong with the For Loop

notes-on-haskell.blogspot.com

1–10 of 59 posts

Re: Notes on Haskell: What's Wrong with the For Loop

#2
Heh, all of these points could easily be applied to Python as well. All the concepts are there in addition to the for loop. It's nice how you can do a lot of functional programming in Python with these simple tools (map, filter and reduce (Haskell's foldl).

I wonder why there aren't more people advocating the use of these functions in Python programs instead of for loops.

Re: Notes on Haskell: What's Wrong with the For Loop

#3
I feel this post is more an argument for first class functions than closures. It is true that much of the usefulness of first class functions is lost without closures, so having useful first class function almost necessitates (though does not guarantee) closures. But in none of the examples is the key attribute of closing over free variables emphasized, which is the powerful and dangerous part.

A simple example showing the usefulness of inner functions might have been more pertinent to the term closure IMO.

Re: Notes on Haskell: What's Wrong with the For Loop

#4
post #2

Heh, all of these points could easily be applied to Python as well. All the concepts are there in addition to the for loop. It's nice how you can do a lot of functional programming in Python with these simple tools (map, filter and reduce (Haskell's foldl). I wonder why there aren't more people advocating the use of these functions in Python programs instead of for loops.

Java's younger cousin C# also has this with Select, Where and Aggregate. But sometimes the foreach and even the for loop actually makes more sense, in particular when dealing with very imperative algorithms or are not acting over a (full) sequence at all.

I'm usually only reading and not writing python but I think list comprehensions are more idiomatic. Based on some comments I've seen here I also kind of get the impression comprhensions are abused.

Re: Notes on Haskell: What's Wrong with the For Loop

#5
post #4
post #2

Heh, all of these points could easily be applied to Python as well. All the concepts are there in addition to the for loop. It's nice how you can do a lot of functional programming in Python with these simple tools (map, filter and reduce (Haskell's foldl). I wonder why there aren't more people advocating the use of these functions in Python programs instead of for loops.

Java's younger cousin C# also has this with Select, Where and Aggregate. But sometimes the foreach and even the for loop actually makes more sense, in particular when dealing with very imperative algorithms or are not acting over a (full) sequence at all. I'm usually only reading and not writing python but I think list comprehensions are more idiomatic. Based on some comments I've seen here I also kind of get the imp…

Well, if you don't want to work with the whole sequence, you can filter it. But yeah, in those cases and some others, the for loop may be more idiomatic and better performing.

Re: Notes on Haskell: What's Wrong with the For Loop

#6
post #5
post #4

Earlier quoted context omitted.

Java's younger cousin C# also has this with Select, Where and Aggregate. But sometimes the foreach and even the for loop actually makes more sense, in particular when dealing with very imperative algorithms or are not acting over a (full) sequence at all. I'm usually only reading and not writing python but I think list comprehensions are more idiomatic. Based on some comments I've seen here I also kind of get the imp…

Well, if you don't want to work with the whole sequence, you can filter it. But yeah, in those cases and some others, the for loop may be more idiomatic and better performing.

Yes I'm talking about being able to stop early for one example. Or if the thing you are dealing with is not actually a sequence for another.

Re: Notes on Haskell: What's Wrong with the For Loop

#7
post #2

Heh, all of these points could easily be applied to Python as well. All the concepts are there in addition to the for loop. It's nice how you can do a lot of functional programming in Python with these simple tools (map, filter and reduce (Haskell's foldl). I wonder why there aren't more people advocating the use of these functions in Python programs instead of for loops.

I've gotten the impression that there are, in a sense - in python, list comprehensions are usually preferred over for-loops in places where you would use map and filter, and the most common use cases for fold/reduce are covered by functions like sum and join. List Comprehensions also tend to be preferred over higher order functions, perhaps for readability, but also, in python 3, the higher order functions return generators, which don't always play nicely with mutable data.

For-loops are still suitable for actions (which mutate state or for some other reason need to be evaluated in order) - you see this in Haskell, too, though, with Control.Monad's forM_ and the like.

Re: Notes on Haskell: What's Wrong with the For Loop

#8
post #2

Heh, all of these points could easily be applied to Python as well. All the concepts are there in addition to the for loop. It's nice how you can do a lot of functional programming in Python with these simple tools (map, filter and reduce (Haskell's foldl). I wonder why there aren't more people advocating the use of these functions in Python programs instead of for loops.

Technically, Python's reduce function is equivalent to a duck-typed version of Haskell's foldl1' function. It's strict, and it has no starting value argument.

(Nitpicking forever!)

Re: Notes on Haskell: What's Wrong with the For Loop

#9
post #6
post #5

Earlier quoted context omitted.

Well, if you don't want to work with the whole sequence, you can filter it. But yeah, in those cases and some others, the for loop may be more idiomatic and better performing.

Yes I'm talking about being able to stop early for one example. Or if the thing you are dealing with is not actually a sequence for another.

The former can probably be solved for lots of cases by TakeWhile(), which evaluates before consuming more elements and could be used to replace "abort the loop" patterns.

The latter is a different thing..

1: http://msdn.microsoft.com/en-us/library/bb534804.aspx

Re: Notes on Haskell: What's Wrong with the For Loop

#10
post #6

Earlier quoted context omitted.

Yes I'm talking about being able to stop early for one example. Or if the thing you are dealing with is not actually a sequence for another.

The former can probably be solved for lots of cases by TakeWhile(), which evaluates before consuming more elements and could be used to replace "abort the loop" patterns. The latter is a different thing.. 1: http://msdn.microsoft.com/en-us/library/bb534804.aspx

[deleted]
Post reply on HN