Live data from Hacker News

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

notes-on-haskell.blogspot.com

31–40 of 59 posts

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

#31
post #17

Earlier quoted context omitted.

There is no check that array[i] actually exists. If array[] has 2 items but args[] has 5, you will overflow the array.

D'oh!!! You're right! I was mentally substituting args[i] for array[i] Talk about code blindness. What a colossal blunder.

I up voted you showed so wonderfully that forcing an fallible human to correctly specify the same thing twice can result in errors.

But I don't think errors are the biggest issue. The biggest issue is that this kind of stuff should be getting out of our way so we can focus on other things.

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

#32

Colour me stupid, but in the article it gives this code as having some horrible hard to find bug: String s = ""; for (int i = 0; i Now, as soon as I looked at that I immediately thought "well, they're not putting anything in between the params when they concatenate them", e.g. I would expect to see: s += array[i] + "\t"; (or a comma or newline instead of a tab) My next thought was "what happens if there are no args?"…

"E.g. args.length returns 0, and they try to access the first element in the list (due to heritage from c pointers we start counting at 0) which is the 'zeroth' one, which doesn't exist"

I'm not sure if you're making some subtle point here that I'm missing, but it is not the case that any of the elements of array will be accessed by that code if args.length is 0. If args.length is 0, then the exit condition (which is evaluated before entering the loop body for the first time) will be false, and the loop body will not be executed at all.

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

#33
post #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!)

Well, as long as we're nitpicking...

Python's reduce function has an optional starting value argument: http://docs.python.org/library/functions.html#reduce

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

#34
post #20

Earlier quoted context omitted.

I've been trying to figure out what he was getting at, as well. The original article, by Elliotte, seems to have updated the code snippet to: ' String s = ""; for (int i = 0; i I deduce from this that the error was in the copy/paste. The line that was supposed to be changed to 's+= args[i]' was accidentally left as 's+= array[i]' As such, its a simple failure to replace all instances of 'array' in the pasted code, ra…

In the interest of being fair, it is (by internet standards) quite an old blog post, which was probably a bit of a toss off effort in the first place. If 'the bug' is just a copy/paste problem of a variable that doesn't exist in that scope, then compilation will fail at that point, so again, I don't see how that could be the one that Turoff said would survive for weeks in the wild.

But, if the bug is a copy/paste of a variable that does exist in that scope, he's exactly correct that it could survive for weeks and be very difficult to actually track down (because of the sort of blindness to details that happens when you read "idioms" like this by visual pattern matching rather than actually reading the code).

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

#35
OK,

Let's imagine there are two kinds of language/program bugs.

1) A given expression does not mean what you think it means. 2) A given series of expression all mean what you think they mean but the combination of them isn't what you think it should be.

I would submit that bug type #2 is the main sort of bug that's going to bite you and bug type #1 is relatively benign (if occasionally infuriating). Especially, even in language with obscure syntax and hairy ambiguity, you can pepper your code with asserts and track down your problem reasonably quickly.

I have been assure that functional programming does help with bug #2. I would love some detailed blog post akin to this which says how.

This article seems to deal entirely with bug type #1. Show me how functional programming deals with bug type #2.

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

#36
post #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 gen…

After reading this, I got to wondering if I could hijack sum into joining lists. My first attempt didn't work:

  sum([ range(n,n+5) for n in range(5) ])
just gave me "TypeError: unsupported operand type(s) for +: 'int' and 'list'", which didn't make a lot of sense. Where did I pass an 'int' by itself? Well, according to the help, sum takes a second argument: a starting value, which defaults to 0. So, I wondered, what if I were to pass it an empty list...?

  sum([ range(n,n+5) for n in range(5) ], [])
came back with:

  [0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8]
Holy $#!7

I'd been wanting a way to join multiple lists without having to write another ugly little function! Anyone know if this particular trick is common practice?

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

#37
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.

[deleted]

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

#38

Colour me stupid, but in the article it gives this code as having some horrible hard to find bug: String s = ""; for (int i = 0; i Now, as soon as I looked at that I immediately thought "well, they're not putting anything in between the params when they concatenate them", e.g. I would expect to see: s += array[i] + "\t"; (or a comma or newline instead of a tab) My next thought was "what happens if there are no args?"…

Why are you looping over array, when you are checking over the bounds of args? Shouldn't it be s += args[i];

Normally, you loop over the thing you're checking the bounds for.

The fact that this has been sitting on HN front page all day and there's no single, satisfactory answer (I mean, is this a satisfactory answer?) is to me a Key Failure of the for loop. For loops just don't capture very much in terms of semantics.

Why are we befuddled by what this piece of code does? You can get into stupid pedantry about what a closure is (yes, looking at the top comment here), and various other details, but that misses the point of the article, which is that for-loop are practically an anti-pattern.

As a community, we've long realized that functions like fold, filter and map are good things, better than our equivalent of the for-loop, which is recursing over the data-structure. You better have a solid reason to write your own pattern match over a list. In all my years of writing functional code, I've only once had to write a function that recursed over a list, and boy was it a regrettable function.

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

#39
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 rarely use for loops in Python. Most of the time I use list comprehensions, which are much more terse and expressive than loops and, at least to me, are easier to parse than map/filter/reduce functions.

Funny, that. List comprehensions are actually sytactically and semantically more difficult than folds, modulo your logic.

Honestly, I suspect the Haskell style and the curious Python cultural disdain for real lambadas have far more to do with your confort than any absolute metric of comprehension.

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

#40
post #36
post #7

Earlier quoted context omitted.

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 gen…

After reading this, I got to wondering if I could hijack sum into joining lists. My first attempt didn't work: sum([ range(n,n+5) for n in range(5) ]) just gave me "TypeError: unsupported operand type(s) for +: 'int' and 'list'", which didn't make a lot of sense. Where did I pass an 'int' by itself? Well, according to the help, sum takes a second argument: a starting value, which defaults to 0. So, I wondered, what i…

That's actually quite clever.
Post reply on HN