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.
Notes on Haskell: What's Wrong with the For Loop
21–30 of 59 posts
Re: Notes on Haskell: What's Wrong with the For Loop
#22Earlier 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.
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…
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.
Re: Notes on Haskell: What's Wrong with the For Loop
#23That word, closure. You keep using it, but I do not think it means what you think it means. A closure is an runtime structure used to implement static scope of locally-defined first-class functions. Closures allow locally-defined functions to "remember" variable bindings in their enclosing scope. Beside that you can implement this feature WITHOUT closures (e.g. source rewriting), none of the examples presented actual…
http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-wit...
"A few people have commented that none of the examples use closures, but in Haskell closures are used not only to represent functions but also unevaluated values (i.e., thunks). For example, in the simple expression
total = sum array
the variable array is free and refers to some value, possibly unevaluated, in the enclosing environment. Until the value of total is evaluated it exists as a thunk that closes over array (and sum, for that matter).
So the examples do indeed use closures.
Cheers! ---Tom"
Re: Notes on Haskell: What's Wrong with the For Loop
#24Colour 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 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.Re: Notes on Haskell: What's Wrong with the For Loop
#25Earlier 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…
"Java's younger cousin C#" We prefer the terms: "brother by a different mother" or if they're being naughty: "red headed step-child".
Re: Notes on Haskell: What's Wrong with the For Loop
#26That word, closure. You keep using it, but I do not think it means what you think it means. A closure is an runtime structure used to implement static scope of locally-defined first-class functions. Closures allow locally-defined functions to "remember" variable bindings in their enclosing scope. Beside that you can implement this feature WITHOUT closures (e.g. source rewriting), none of the examples presented actual…
Lambda expressions are to closures as `new' expressions are to instances: a lambda expression evaluates to a closure, and a `new' expression evaluates to an instance. Since any expression can be evaluated multiple times, in general the relationship is one-to-many: a lambda expression can be evaluated N times to produce N closures, just as a `new' expression can be evaluated N times to produce N instances.
(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.)
Re: Notes on Haskell: What's Wrong with the For Loop
#27That word, closure. You keep using it, but I do not think it means what you think it means. A closure is an runtime structure used to implement static scope of locally-defined first-class functions. Closures allow locally-defined functions to "remember" variable bindings in their enclosing scope. Beside that you can implement this feature WITHOUT closures (e.g. source rewriting), none of the examples presented actual…
I'm not the author, and I think this was kind of a cop out, but: http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-wit... "A few people have commented that none of the examples use closures, but in Haskell closures are used not only to represent functions but also unevaluated values (i.e., thunks). For example, in the simple expression total = sum array the variable array is free and refers to some value, poss…
Re: Notes on Haskell: What's Wrong with the For Loop
#28There'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 programming. Fortunately.
Re: Notes on Haskell: What's Wrong with the For Loop
#29That word, closure. You keep using it, but I do not think it means what you think it means. A closure is an runtime structure used to implement static scope of locally-defined first-class functions. Closures allow locally-defined functions to "remember" variable bindings in their enclosing scope. Beside that you can implement this feature WITHOUT closures (e.g. source rewriting), none of the examples presented actual…
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…
Re: Notes on Haskell: What's Wrong with the For Loop
#30That word, closure. You keep using it, but I do not think it means what you think it means. A closure is an runtime structure used to implement static scope of locally-defined first-class functions. Closures allow locally-defined functions to "remember" variable bindings in their enclosing scope. Beside that you can implement this feature WITHOUT closures (e.g. source rewriting), none of the examples presented actual…
http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-wit...
In Haskell all parameters are lazy by default, which effectively turns every parameter into a closure.
for e.g., consider the code
boost x = map (x) [1..10]
total = sum (boost 2)
On invocation of the sum function, the (boost 2) is not necessarily evaluated. Instead a reference to ((x -> map (x) [1..10]) 2) is passed in to sum. So sum will now end up actually accessing x which has been bound to the value 2. When written this way, the binding of x to 2 is more explicit, but this is the way Haskell evaluates expressions and function calls by default. So (boost 2) might not actually look like a closure, but it effectively is.
Thanks to Haskell's purity, GHC can actually turn this expression into a plain for loop with no lists being constructed. At least in the context of Haskell, this makes discussions of the "runtime structure that represents a closure" meaningless as there may be no run time structures at all, not even a list, much less a closure.
Any value, which doesn't even look like a function/closure could be referencing an unevaluated function with any kind of "variable"[1] bindings.
[1] - Haskell is pure and does not have variables, only constants and functions.