fold

Implement zip using foldr

北城余情 提交于 2019-11-28 09:03:22
I'm currently on chapter 4 of Real World Haskell, and I'm trying to wrap my head around implementing foldl in terms of foldr . (Here's their code:) myFoldl :: (a -> b -> a) -> a -> [b] -> a myFoldl f z xs = foldr step id xs z where step x g a = g (f a x) I thought I'd try to implement zip using the same technique, but I don't seem to be making any progress. Is it even possible? zip2 xs ys = foldr step done xs ys where done ys = [] step x zipsfn [] = [] step x zipsfn (y:ys) = (x, y) : (zipsfn ys) How this works: (foldr step done xs) returns a function that consumes ys; so we go down the xs list

How can operations like map, filter and reverse can be defined in terms of a reduce?

假装没事ソ 提交于 2019-11-28 06:05:33
问题 In this blog entry, "CSP and transducers in JavaScript", the author states: First, we have to realise that many array (or other collection) operations like map , filter and reverse can be defined in terms of a reduce . My question is: How can operations like map, filter and reverse can be defined in terms of a reduce? Could you provide examples in Clojure? 回答1: This is true, if we don't care about laziness. In Clojure, map and filter are lazy, but reduce is eager. Not only is reverse not lazy

Left and Right Folding over an Infinite list

ⅰ亾dé卋堺 提交于 2019-11-28 03:32:06
I have issues with the following passage from Learn You A Haskell (Great book imo, not dissing it): One big difference is that right folds work on infinite lists, whereas left ones don't! To put it plainly, if you take an infinite list at some point and you fold it up from the right, you'll eventually reach the beginning of the list. However, if you take an infinite list at a point and you try to fold it up from the left, you'll never reach an end! I just don't get this. If you take an infinite list and try to fold it up from the right then you'll have to start at the point at infinity, which

Haskell - foldl and foldr?

大兔子大兔子 提交于 2019-11-28 03:08:24
Is difference between foldl and foldr just the direction of looping? I thought there was a difference in what they did, not just in the direction? AndrewC There's a difference if your function isn't associative (i.e. it matters which way you bracket expressions) so for example, foldr (-) 0 [1..10] = -5 but foldl (-) 0 [1..10] = -55 . On a small scale, this is because 10-(20-(30)) isn't the same as ((10)-20)-30 . Whereas because (+) is associative (doesn't matter what order you add subexpressions), foldr (+) 0 [1..10] = 55 and foldl (+) 0 [1..10] = 55 . (++) is another associative operation

An example of a Foldable which is not a Functor (or not Traversable)?

徘徊边缘 提交于 2019-11-27 18:27:34
A Foldable instance is likely to be some sort of container, and so is likely to be a Functor as well. Indeed, this says A Foldable type is also a container (although the class does not technically require Functor , interesting Foldable s are all Functor s). So is there an example of a Foldable which is not naturally a Functor or a Traversable ? (which perhaps the Haskell wiki page missed :-) ) Sjoerd Visscher Here's a fully parametric example: data Weird a = Weird a (a -> a) instance Foldable Weird where foldMap f (Weird a b) = f $ b a Weird is not a Functor because a occurs in a negative

Cross Validation in Weka

百般思念 提交于 2019-11-27 18:22:39
I've always thought from what I read that cross validation is performed like this: In k-fold cross-validation, the original sample is randomly partitioned into k subsamples. Of the k subsamples, a single subsample is retained as the validation data for testing the model, and the remaining k − 1 subsamples are used as training data. The cross-validation process is then repeated k times (the folds), with each of the k subsamples used exactly once as the validation data. The k results from the folds then can be averaged (or otherwise combined) to produce a single estimation So k models are built

Interleave List of Lists in Haskell

北战南征 提交于 2019-11-27 18:17:14
问题 I was wondering how could I write a function in Haskell that interleaves a list of lists into a single lists, for example, if I had a function called interleavelists :: [[a]] -> [a] it should be able to interleave the elements. Example: [[1,2,3] [4,5,6] [7,8]] --> [1,4,7,2,5,8,3,6] . The lists can be both finite or infinite... Can I use foldr ? 回答1: The quickest way to write it is to use transpose from Data.List . import Data.List interleavelists :: [[a]] -> [a] interleavelists = concat .

What is the 'pythonic' equivalent to the 'fold' function from functional programming?

和自甴很熟 提交于 2019-11-27 17:46:02
What is the most idiomatic way to achieve something like the following, in Haskell: foldl (+) 0 [1,2,3,4,5] --> 15 Or its equivalent in Ruby: [1,2,3,4,5].inject(0) {|m,x| m + x} #> 15 Obviously, Python provides the reduce function, which is an implementation of fold, exactly as above, however, I was told that the 'pythonic' way of programming was to avoid lambda terms and higher-order functions, preferring list-comprehensions where possible. Therefore, is there a preferred way of folding a list, or list-like structure in Python that isn't the reduce function, or is reduce the idiomatic way of

Why is the fold action necessary in Spark?

只谈情不闲聊 提交于 2019-11-27 13:22:58
I've a silly question involving fold and reduce in PySpark . I understand the difference between these two methods, but, if both need that the applied function is a commutative monoid, I cannot figure out an example in which fold cannot be substituted by reduce`. Besides, in the PySpark implementation of fold it is used acc = op(obj, acc) , why this operation order is used instead of acc = op(acc, obj) ? (this second order sounds more closed to a leftFold to me) Cheers Tomas Empty RDD It cannot be substituted when RDD is empty: val rdd = sc.emptyRDD[Int] rdd.reduce(_ + _) // java.lang

Scala : fold vs foldLeft

主宰稳场 提交于 2019-11-27 12:34:43
I am trying to understand how fold and foldLeft and the respective reduce and reduceLeft work. I used fold and foldLeft as my example scala> val r = List((ArrayBuffer(1, 2, 3, 4),10)) scala> r.foldLeft(ArrayBuffer(1,2,4,5))((x,y) => x -- y._1) scala> res28: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5) scala> r.fold(ArrayBuffer(1,2,4,5))((x,y) => x -- y._1) <console>:11: error: value _1 is not a member of Serializable with Equals r.fold(ArrayBuffer(1,2,4,5))((x,y) => x -- y._1) Why fold didn't work as foldLeft ? What is Serializable with Equals ? I understand fold and foldLeft has