4 ms·
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 -
by mbrubeck 17y ago
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/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.