fold

Does haskell's foldr always take a two-parameter lambda?

坚强是说给别人听的谎言 提交于 2019-12-04 03:51:01
Haskell newb here I'm working on this problem in haskell: (**) Eliminate consecutive duplicates of list elements. If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed. Example: * (compress '(a a a a b c c a a d e e e e)) (A B C A D E) The solution (which I had to look up) uses foldr: compress' :: (Eq a) => [a] -> [a] compress' xs = foldr (\x acc -> if x == (head acc) then acc else x:acc) [last xs] xs This foldr, according to the solution, takes two parameters, x and acc. It would seem like all foldr's

What would be the “distinct method” that Traversable has in addition to Foldable?

孤街浪徒 提交于 2019-12-04 03:50:38
Foldable is a superclass of Traversable , similarly to how Functor is a superclass of Applicative and Monad . Similar to the case of Monad , where it is possible to basically implement fmap as liftM :: Monad m => (a->b) -> m a -> m b liftM f q = return . f =<< q we could also emulate foldMap as foldLiftT :: (Traversable t, Monoid m) => (a -> m) -> t a -> m foldLiftT f = fst . traverse (f >>> \x -> (x,x)) -- or: . sequenceA . fmap (f >>> \x -> (x, x)) using the Monoid m => (,) m monad. So the combination of superclass and methods bears in both cases a certain redundancy. In case of monads, it

Are fold expressions subject to short-circuiting?

送分小仙女□ 提交于 2019-12-04 03:45:22
问题 In C++17, are fold expressions subject to short-circuiting when used with && or || as their operator? If so, where is this specified? 回答1: Yes, fold expressions using && or || as the operator can short-circuit, subject to the usual caveat that it happens for the built-in meaning, but not for an overloaded operator function. The meaning of a fold-expression is defined in [temp.variadic]/9: The instantiation of a fold-expression produces: ((E_1 op E_2) op ... ) op E_N for a unary left fold, E_1

“Any function on finite lists that is defined by pairing the desired result with the argument list can always be redefined in terms of fold”

旧城冷巷雨未停 提交于 2019-12-04 02:13:30
I was reading through the paper A tutorial on the universality and expressiveness of fold , and am stuck on the section about generating tuples. After showing of how the normal definition of dropWhile cannot be defined in terms of fold, an example defining dropWhile using tuples was proved: dropWhile :: (a -> Bool) -> [a] -> [a] dropWhile p = fst . (dropWhilePair p) dropWhilePair :: (a -> Bool) -> [a] -> ([a], [a]) dropWhilePair p = foldr f v where f x (ys,xs) = (if p x then ys else x : xs, x : xs) v = ([], []) The paper states: In fact, this result is an instance of a general theorem

How is foldl lazy?

我的梦境 提交于 2019-12-04 00:45:26
There are lots of good questions and answers about foldl , foldr , and foldl' in Haskell. So now I know that: 1) foldl is lazy 2) don't use foldl because it can blow up the stack 3) use foldl' instead because it is strict ( ish ) How foldl is evaluated: 1) a whole bunch of thunks are created 2) after Haskell is done creating thunks, the thunks are reduced 3) overflow the stack if there are too many thunks What I'm confused about: 1) why does reduction have to occur after all thunk-ing? 2) why isn't foldl evaluated just like foldl' ? Is this just an implementation side-effect? 3) from the

Scala Vector fold syntax (/: and :\\ and /:\\)

霸气de小男生 提交于 2019-12-03 23:24:25
Can someone provide some examples for how /: :\ and /:\ Actually get used? I assume they're shortcuts to the reduce / fold methods, but there's no examples on how they actually get used in the Scala docs, and they're impossible to google / search for on StackOverflow. /: is a synonym for foldLeft and :\ for foldRight . But remember that : makes /: apply to the object to the right of it. Assuming you know that (_ * _) is an anonymous function that's equivalent to (a, b) => a * b , and the signature of foldLeft and foldRight are def foldLeft [B] (z: B)(f: (B, A) ⇒ B): B def foldRight [B] (z: B)

Is foldl ever preferable to its strict cousin, foldl'?

百般思念 提交于 2019-12-03 22:19:04
Haskell has two left fold functions for lists: foldl , and a "strict" version, foldl' . The problem with the non-strict foldl is that it builds a tower of thunks: foldl (+) 0 [1..5] --> ((((0 + 1) + 2) + 3) + 4) + 5 --> 15 This wastes memory, and may cause a stack overflow if the list has too many items. foldl' , on the other hand, forces the accumulator on every item. However, as far as I can tell, foldl' is semantically equivalent to foldl . Evaluating foldl (+) 0 [1..5] to head normal form requires forcing the accumulator at some point. If we didn't need a head-normal form, we wouldn't be

Collapsible header in Markdown to html

北城余情 提交于 2019-12-03 16:07:23
问题 Our internal git-lab wiki works with Markdown. I made several summaries of articles and want to post them in our wiki, in such a way that if I click on the header, it should unfold and the text should become visible, basically like in this example Does Markdown have this expand/collapse/fold function? 回答1: Short Answer: No, Markdown does not offer a feature like that directly, but with some work you might be able to build something that works. For a feature like that to work you would need

Applying logical and to list of boolean values

吃可爱长大的小学妹 提交于 2019-12-03 15:33:16
问题 Consider the following list of Boolean values in Scala List(true, false, false, true) How would you using either foldRight or foldLeft emulate the function of performing a logical AND on all of the values within the list? 回答1: val l = List(true, false, false, true) val andAll = l.foldLeft(true)(_ && _) 回答2: Instead of using foldLeft/Right , you can also use forall(identity) for the logical AND, or exists(identity) for the logical OR. edit: The benefit of these functions is the early exit. If

How to fold STL container?

我怕爱的太早我们不能终老 提交于 2019-12-03 14:40:45
问题 I need an analog of Haskell's foldl function to fold any STL containers. Expected signature is like following: template Iterator, FoldingFunction, Result Result foldl( Iterator begin, Iterator end, FoldingFunction f, Result initValue); Standard STL has no such function. Does Boost have any? I know it's pretty simple to implement, but I'd like to know whether there's any ready standardized implementation. And one more question: how do you usually fold data lists in C++/STL? 回答1: STL does have