Live data from Hacker News

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

notes-on-haskell.blogspot.com

51–59 of 59 posts

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

#52

You can execute loop in your head just by reading it line by line. Token by token. With other solutions you need to know other things to determine what goes inside and believe it is actually what you want it to be. Loops ar flat, explicit and versatile. I think that is the reason behind their popularity.

If you're familiar with foldr or its ilk you can execute them in your head token by token too, to the same extent you can in a for loop. In the case of a for loop you have to be familiar with the syntax and semantics of "for" in your language so you know that "for(i = 0; i<n; i++)" is different from "for(i = 0; i++; i < n)" and likewise you have to be familiar with the syntax and semantics of any functional construct you use.

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

#53
post #28

"But it does highlight the key failing of for loops: they conflate three separate kinds of operations -- filtering, reduction and transformation." There's actually four kinds of operations: filtering, reduction, transformation, and good Lord man what the hell are you doing with the loop index? did it just go negative? It did! Why? And it still works‽ , which is actually quite hard to simulate with functional programm…

I can top that mine went up to 17 and then jumped to 200 for no obvious reason(some sort of memory bug where memory in a different part of the program overlapped with the loop variable).

Did i still work? A bug isn't a fundamental kind of loop operation.

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

#54
post #41

Earlier quoted context omitted.

Yes, this has been one of my pet peeves for a while too. Everybody: a closure, as the parent says, is an implementation construct. It is not something you can find in your source code. The syntactic construct -- the thing you write in your code -- is called a lambda expression . Not a "lambda function", and not a "closure"! Lambda expressions are to closures as `new' expressions are to instances: a lambda expression…

> Instances and closures are closely related: an instance is a piece of state with several operations that can be invoked on it, while a closure is a piece of state with one operation that can be invoked on it. I would go further, and say that they're equivalent—that "one operation" can be a dispatch function: def make_object x = 5 lambda do |m| case m when 'increment' x += 1 when 'decrement' x -= 1 when 'get' x end…

In fact, in SICP, they build their object system in this way, if my memory serves me correctly.

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

#55

You can execute loop in your head just by reading it line by line. Token by token. With other solutions you need to know other things to determine what goes inside and believe it is actually what you want it to be. Loops ar flat, explicit and versatile. I think that is the reason behind their popularity.

If you're familiar with foldr or its ilk you can execute them in your head token by token too, to the same extent you can in a for loop. In the case of a for loop you have to be familiar with the syntax and semantics of "for" in your language so you know that "for(i = 0; i<n; i++)" is different from "for(i = 0; i++; i < n)" and likewise you have to be familiar with the syntax and semantics of any functional construct…

To understand loop you just need to know in which order to execute its parts. And you see how it reduces, maps an filters your data. You don't need to keep stack in your head. To understand how functional constructs work you have to know a lot more because they are specialised and it's not that easy to track what they exactly do because of recursion. You have to know them and trust them. With loop all internal logic is linear and in plain sight.

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

#56
post #41

Earlier quoted context omitted.

> Instances and closures are closely related: an instance is a piece of state with several operations that can be invoked on it, while a closure is a piece of state with one operation that can be invoked on it. I would go further, and say that they're equivalent—that "one operation" can be a dispatch function: def make_object x = 5 lambda do |m| case m when 'increment' x += 1 when 'decrement' x -= 1 when 'get' x end…

In fact, in SICP, they build their object system in this way, if my memory serves me correctly.

Of course. Closures are, after all, a poor man's objects[1].

[1] http://codermonk.blogspot.com/2007/01/venerable-master-qc-na...

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

#57

Earlier quoted context omitted.

If you're familiar with foldr or its ilk you can execute them in your head token by token too, to the same extent you can in a for loop. In the case of a for loop you have to be familiar with the syntax and semantics of "for" in your language so you know that "for(i = 0; i<n; i++)" is different from "for(i = 0; i++; i < n)" and likewise you have to be familiar with the syntax and semantics of any functional construct…

To understand loop you just need to know in which order to execute its parts. And you see how it reduces, maps an filters your data. You don't need to keep stack in your head. To understand how functional constructs work you have to know a lot more because they are specialised and it's not that easy to track what they exactly do because of recursion. You have to know them and trust them. With loop all internal logic…

Its certainly true that to understand a fold at the abstraction level that C uses you have to know more, but that isn't the same as you having to know it in order to program effectively. The point of abstraction is that you don't need to know the fine details of whats happening under the hood. You might talk about how, in a C program you can watch what happens to your loop variable as you execute, but really "variables" are just abstractions over what the machine is really doing. If you were to look at the assembly spit out by your compiler that loop variable might be in a register or on the stack or in some combination thereof, and if your compiler does loop unrolling it might have different values at the same time even in the assembly. And once you get into the reorder buffer or a modern CPU I guarantee you that what actually happens will bear very little resemblance to what you would naively think if you just looked at the C code.

So you can execute a loop C-token by C-token and you can execute a fold Haskel-token by Haskel-token, but both are abstractions high above whats happening at the machine level and I don't see why we should prefer one over the other.

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

#58

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?"…

"What am I missing?" What a noob! for (int i = 0; i If args.length is zero, the body of the loop won't run, since 0 < 0 is false. Duh.

Parent didn't need the downmods - it's the same poster replying to himself with "what a noob". I was about to post this same answer till I saw this at the bottom of the replies.

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

#59
post #58

Earlier quoted context omitted.

"What am I missing?" What a noob! for (int i = 0; i If args.length is zero, the body of the loop won't run, since 0 < 0 is false. Duh.

Parent didn't need the downmods - it's the same poster replying to himself with "what a noob". I was about to post this same answer till I saw this at the bottom of the replies.

"Parent didn't need the downmods"

It's okay. I continue to be baffled and amazed at what gets voted up and what gets voted down, usually it balances out, so if something gets unexpectedly voted down, something else will get unexpectedly voted up.

Post reply on HN