fold

Why can you reverse list with foldl, but not with foldr in Haskell

有些话、适合烂在心里 提交于 2019-12-03 12:21:08
问题 Why can you reverse a list with the foldl? reverse' :: [a] -> [a] reverse' xs = foldl (\acc x-> x : acc) [] xs But this one gives me a compile error. reverse' :: [a] -> [a] reverse' xs = foldr (\acc x-> x : acc) [] xs Error Couldn't match expected type `a' with actual type `[a]' `a' is a rigid type variable bound by the type signature for reverse' :: [a] -> [a] at foldl.hs:33:13 Relevant bindings include x :: [a] (bound at foldl.hs:34:27) acc :: [a] (bound at foldl.hs:34:23) xs :: [a] (bound

Code folding is not saved in my vimrc

六月ゝ 毕业季﹏ 提交于 2019-12-03 10:30:17
I added the following code to my .vimrc: " save and restore folds when a file is closed and re-opened autocmd BufWinLeave *.* mkview autocmd BufWinEnter *.* silent loadview HTML and CSS documents save and restore their folds but code folding is not being saved in my .vimrc Any suggestions? EDIT: The following code solves the problem: au BufWinLeave ?* mkview au BufWinEnter ?* silent loadview but if I write it, the MRU files disappear from my list (and I have to open MRU twice in order to see my list of recent files why?) The problem is that your original autocmd lines are set to match the

Fold expressions with arbitrary callable?

我的梦境 提交于 2019-12-03 10:23:21
Looking over the C++17 paper on folds, (and on cppreference ), I'm confused as to why the choice was made to only work with operators? At first glance it seems like it would make it easier to expand (... + args) by just shoving a + token between the elements of args , but I'm unconvinced this is a great decision. Why can't a binary lambda expression work just as well and follow the same expansion as the latter above? It's jarring to me that a fold syntax would be added to a language without support for arbitrary callables, so does the syntax allow a way to use them that I'm just not seeing?

Multiple folds in one pass using generic tuple function

末鹿安然 提交于 2019-12-03 06:52:26
How can I write a function which takes a tuple of functions of type ai -> b -> ai and returns a function which takes a tuple of elements of type ai , one element of type b , and combines each of the elements into a new tuple of ai : That is the signature should be like f :: (a1 -> b -> a1, a2 -> b -> a2, ... , an -> b -> an) -> (a1, a2, ... , an) -> b -> (a1, a2, ... , an) Such that: f (min, max, (+), (*)) (1,2,3,4) 5 = (1, 5, 8, 20) The point of this is so I can write: foldlMult' t = foldl' (f t) And then do something like: foldlMult' (min, max, (+), (*)) (head x, head x, 0, 0) x to do

Applying logical and to list of boolean values

社会主义新天地 提交于 2019-12-03 05:03:23
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? val l = List(true, false, false, true) val andAll = l.foldLeft(true)(_ && _) 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 forall hits a false or exists a true , they will immediately return. Without initial value as in foldLeft , List

Collapsible header in Markdown to html

喜欢而已 提交于 2019-12-03 04:39: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? 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 some CSS and/or JavaScript to control the animations, etc. While you might be able to get such a feature to

Why can you reverse list with foldl, but not with foldr in Haskell

假装没事ソ 提交于 2019-12-03 02:47:38
Why can you reverse a list with the foldl? reverse' :: [a] -> [a] reverse' xs = foldl (\acc x-> x : acc) [] xs But this one gives me a compile error. reverse' :: [a] -> [a] reverse' xs = foldr (\acc x-> x : acc) [] xs Error Couldn't match expected type `a' with actual type `[a]' `a' is a rigid type variable bound by the type signature for reverse' :: [a] -> [a] at foldl.hs:33:13 Relevant bindings include x :: [a] (bound at foldl.hs:34:27) acc :: [a] (bound at foldl.hs:34:23) xs :: [a] (bound at foldl.hs:34:10) reverse' :: [a] -> [a] (bound at foldl.hs:34:1) In the first argument of `(:)',

FoldLeft using FoldRight in scala

徘徊边缘 提交于 2019-12-03 01:58:29
问题 While going through Functional Programming in Scala, I came across this question: Can you right foldLeft in terms of foldRight? How about the other way around? In solution provided by the authors they have provided an implementation as follows: def foldRightViaFoldLeft_1[A,B](l: List[A], z: B)(f: (A,B) => B): B = foldLeft(l, (b:B) => b)((g,a) => b => g(f(a,b)))(z) def foldLeftViaFoldRight[A,B](l: List[A], z: B)(f: (B,A) => B): B = foldRight(l, (b:B) => b)((a,g) => b => g(f(b,a)))(z) Can

FoldLeft using FoldRight in scala

核能气质少年 提交于 2019-12-02 15:48:37
While going through Functional Programming in Scala , I came across this question: Can you right foldLeft in terms of foldRight? How about the other way around? In solution provided by the authors they have provided an implementation as follows: def foldRightViaFoldLeft_1[A,B](l: List[A], z: B)(f: (A,B) => B): B = foldLeft(l, (b:B) => b)((g,a) => b => g(f(a,b)))(z) def foldLeftViaFoldRight[A,B](l: List[A], z: B)(f: (B,A) => B): B = foldRight(l, (b:B) => b)((a,g) => b => g(f(b,a)))(z) Can somebody help me trace through this solution and make me understand how this actually gets the foldl

Implementing Haskell's `take` function using `foldl`

 ̄綄美尐妖づ 提交于 2019-12-02 15:42:49
问题 Implementing Haskell's take and drop functions using foldl . Any suggestions on how to implement take and drop functions using foldl ?? take x ls = foldl ??? drop x ls = foldl ??? i've tried these but it's showing errors: myFunc :: Int -> [a] -> [a] myFunc n list = foldl func [] list where func x y | (length y) > n = x : y | otherwise = y ERROR PRODUCED : *** Expression : foldl func [] list *** Term : func *** Type : a -> [a] -> [a] *** Does not match : [a] -> [a] -> [a] *** Because :