News for hackers. Stuff that matters.
Re:Guido on Functional by jrockway on Monday March 08, 2010 @01:03PM (#1175916)
Guido is exactly right. reduce is one line of code, not "a few" lines of code.
Re:Guido on Functional by mbrubeck on Monday March 08, 2010 @02:16PM (#1176137)
The Haskell 98 Report takes two lines:

    foldr f z []     =  z
    foldr f z (x:xs) =  f x (foldr f z xs)
Clojure does it like this (note the overloading - this is both "foldl" and "foldl1" in Haskell terms): http://github.com/richhickey/clojure/blob/49a7d6b8e14050a45d...
Re:Guido on Functional by eru on Monday March 08, 2010 @02:19PM (#1176143)
To be true, it's customary that you also add a line for the types in Haskell. Though not necessary.
Re:Guido on Functional by jdeseno on Monday March 08, 2010 @05:20PM (#1176664)
Couldn't we do this?

r' f (x:xs) | xs == [] = (f x):[] | otherwise = (f x):(r' f xs)

or:

[(f a) | a <- myList]

- another edit -

r' f (x:xs) | xs == [] = x | otherwise = f x (r' f xs)

Re:Guido on Functional by mbrubeck on Monday March 08, 2010 @05:24PM (#1176674)
That's map, not reduce.
[deleted]
Re:Guido on Functional by mbrubeck on Monday March 08, 2010 @05:35PM (#1176702)
That's filter, not reduce. :)
Re:Guido on Functional by jganetsk on Monday March 08, 2010 @07:56PM (#1177083)
He's right with the second one.

r' f (x:xs) | xs == [] = x | otherwise = f x (r' f xs)

r' :: (Eq t) => (t -> t -> t) -> [t] -> t

That's definitely not filter. That's definitely reduce. (I'm equating reduce with foldr1 or foldl1). Now, because he used the == operator, that adds the (Eq t) constraint. We can rewrite his code as the following...

r' f (x:xs) = case xs of {[] -> x; _ -> f x (r' f xs)}

r' :: (t -> t -> t) -> [t] -> t

Try actually running it on some code.

r' (+) [1,2,3,4] evaluates to 10

Re:Guido on Functional by mbrubeck on Monday March 08, 2010 @09:29PM (#1177298)
The "filter" version was deleted and the correct version added, after I wrote my comment.
Re:Guido on Functional by Confusion on Monday March 08, 2010 @01:22PM (#1175962)
SICP defines fold-right in 5 lines, so I'd rather say he's just wrong.
Re:Guido on Functional by ubernostrum on Monday March 08, 2010 @02:43PM (#1176216)
Well, to be really fair, most of the people I've seen trying to prove him wrong ended up implementing it as, basically, "let my_reduce = reduce", or something similar but obfuscated enough to make it not look completely tautologous. Which is really the point: a lot of the modern FP languages make folds and other similar constructs so fundamental to the language ("primitives" in terms of the quote) that it can be difficult -- either due to mindset or due to the language itself getting in the way -- to drop down a level or two on the abstraction ladder and try different approaches.

(and the fact that everybody who hasn't implemented a tautologous reduce has basically used the same tail-recursive algorithm lends a bit more support to this theory)

The more I think about it, it's just another expression of the old "when all you have is a hammer" aphorism. Guido's being yelled at and called names not because he's wrong or because he's stupid or doesn't understand what he's talking about, he's being yelled at and called names because the "hammer" in this case -- functional programming -- is a bit of a sacred cow, and a lot of people get really defensive about it.

Re:Guido on Functional by mbrubeck on Monday March 08, 2010 @02:57PM (#1176255)
Fold/reduce is not a primitive in Clojure, Erlang, Haskell, SML, Mercury, or any other functional language I know (though it is a standard library function in all of them). Do you have any counter-examples?

Of course all functional programmers write the same tail-recursive implementation of "reduce." That's like observing that everyone uses a loop to implement "reduce" in an imperative language. It's the simple, straightforward way to do it. (And since folds are trivially implemented in any functional language with tail recursion, why would the language designer include them as a primitive?)

Re:Guido on Functional by ubernostrum on Monday March 08, 2010 @03:16PM (#1176295)
I took "primitive" to mean not only language syntax, but built-in functions which are so fundamental to programming in a given language that it's difficult to escape them. For, say, Haskell, that's quite true of folds.
Re:Guido on Functional by mbrubeck on Monday March 08, 2010 @03:26PM (#1176317)
I don't think it's true in Haskell at all. A fold is just walking a list with an accumulator. That's not weird or low-level; it's as common for Haskell programmers as loops and variables for C programmers. It actually took me a while (when first learning SML and Scheme) to realize I could use the built-in reduce functions more, instead of writing loops "by hand" all the time.

It's also very common in Haskell to implement your own fold for various reasons. For example, in a simple 50-line text processing script[1], I defined a fold for a list-of-trees data structure without calling any builtin fold function[2].

[1]: http://github.com/mbrubeck/outline-grep

[2]: http://github.com/mbrubeck/outline-grep/blob/master/OutlineG...

P.S. I like your blog, and huge thanks for Django!

Re:Guido on Functional by jrockway on Monday March 08, 2010 @04:42PM (#1176540)
Actually, a fold is walking a Foldable with an accumulator :)