7 ms·
Guido on Functional
- jrockway 17y agoGuido is exactly right. reduce is one line of code, not "a few" lines of code.
- Confusion 17y agoSICP defines fold-right in 5 lines, so I'd rather say he's just wrong.
- mbrubeck 17y agoThe 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/49a7d6b8e14050a45df5332e768ba6647752215d/src/clj/clojure/core.clj#L691 http://github.com/richhickey/clojure/blob/49a7d6b8e14050a45d...
- eru 17y agoTo be true, it's customary that you also add a line for the types in Haskell. Though not necessary.
- jdeseno 17y agoCouldn'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)
- mbrubeck 17y agoThat's map, not reduce.
- deleted 17y ago[deleted]
- mbrubeck 17y agoThat's filter, not reduce. :)
- jganetsk 17y agoHe'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
- mbrubeck 17y agoThe "filter" version was deleted and the correct version added, after I wrote my comment.
- deleted 17y ago[deleted]
- deleted 17y ago[deleted]
- ubernostrum 17y agoWell, 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.
- mbrubeck 17y agoFold/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?)
- ubernostrum 17y agoI 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.
- mbrubeck 17y ago
- mainland 17y agoAs much as I like Python, Guido has repeatedly demonstrated a pretty fundamental lack of understanding when it comes to functional programming concepts (I'm thinking in particular of past HN discussions regarding his confusion about the difference between tail calls and tail recursion). While it's entirely appropriate to take the stance that Python should not become "more functional" (whatever that means), it's not appropriate to do so without a clear understanding of what you're talking about. For the record, here's a link to the Haskell Prelude version of reduce, aka, foldr. Looks like "a few lines" to me... http://haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/src/GHC-Base.html#foldr http://haskell.org/ghc/docs/latest/html/libraries/base-4.2.0...
- Xichekolas 17y agoIn looking at that, I was surprised to see that the foldr/build deforestation technique is still in use. That was part of my advisor's PhD work from the mid 90s.
- JoelMcCracken 17y agoIf I had to guess, he thinks this way because he isn't yet used to thinking about space-free tail calls. In that sense, he's correct -- you really couldn't create a usable reduce if the function grew linearly in the size of the list. However, this is not a restriction that we actually face. It amazes me how some programming language authors seem to be clueless about really major areas of our field. If I ever were to design a language, it would only be after studying all of the various interesting programming languages. How else can you be certain that you aren't doing something that hasn't been done before?
- eru 17y agoJust be glad Guido knows how to write a parser.
- mahmud 17y agoBe glad the author of a major programming language has the decency to read Levine and the first 3 chapters of the dragon book? O industrial PL designers, how easily we let you off the hook. I think the guy who had the misfortune to create newLisp caught more flack than Guido, Matz and Larry combined.
- jrockway 17y agoIf you want a lot of people to hate your language, name it "somethingLisp". The CL people will hate you because it's not CL, the Scheme people will hate you because it's not Scheme, and everyone else will hate you because it has parentheses.
- mahmud 17y agoISLisp. Find anyone who hates it :-) This is another standardized Lisp that even Common Lispers and Schemers respect. The ISLispers were the Trotskyists persecuted by the DoD backed CL Stalinist politburo. They had a beautiful Lisp that reconciled all the Lisp differences without sacrificing good taste. However, it was a high-level design that left too many details to the whims of implementers, and would have been impossible to stay compatible to without active and deliberate cooperation. There you have it. And I must go now to board a chopper to to an Alaskan rehab/work program for my reactionary accusations against the U.S. Department of Defense, and the venerable X3J13 ;-)
- va_coder 17y agoGuido works at Google. I wonder if Google is looking at functional languages like Clojure.
- zephjc 17y agoGoogle is large enough that I assume they keep an eye on every language of note. As I understand it, they use a number of different languages for their various needs.
- mahmud 17y agoGoogle is not the homogeneous or coherent; IMO, they've transcended languages by standardizing on communication protocols, which are far more important.