fold

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

我的梦境 提交于 2019-12-02 11:35:47
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 : unification would give infinite type Can't be done. Left fold necessarily diverges on infinite lists, but take

Using reduce over a tree in Lisp

北战南征 提交于 2019-12-02 04:44:18
To fold a flat list in Lisp you use reduce : * (reduce #'+ '(1 2 3 4 5)) 15 But what if I have an arbitrarily complex tree, and I want to apply a function over each of the element? So that fold over '(1 (2) (3 (4) 5)) would still give 15 ? I tried to use reduce , but I had to provide a custom function, which kinda defeats the purpose: (defun tree+ (a b) (cond ((null b) 0) ((atom b) (+ a b)) (t (+ (tree+ a (car b)) (tree+ 0 (cdr b)))))) (reduce #'tree+ '(1 (2) (3 (4) 5)) :initial-value 0) ; returns 15 Of course I could flatten the list first, but reduce is a general function, sometimes you must

Defining foldl in terms of foldr

六眼飞鱼酱① 提交于 2019-12-01 22:18:51
问题 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 am currently reading a book on Haskell. And in it, it wrote its own version of the foldl function, but in terms of foldr. I do not follow. Why are there 4 arguments for foldr? What does the id function do? 回答1: The thing will be become obvious when to expand the expression of foldr step id xs z : As Adam Smith said in the comments: foldr step id xs z = (foldr step id xs) z Consider

Implementation of inits using foldr

两盒软妹~` 提交于 2019-12-01 22:15:58
问题 I have to implement inits via foldr using map. I got most of it, however I'm missing the empty list element in my result list. inits :: [a] -> [[a]] inits = foldr ( \ x y -> [x] : (map (x:) y) ) [] When called this results in: *Blueprint< inits [1,2,3] [[1],[1,2],[1,2,3]] I am a bit stuck now and would be glad if someone could point me in the general direction of my error. Thanks in advance Solved: inits :: [a] -> [[a]] inits = foldr ( \ x y -> [] : (map (x:) y) ) [[]] 回答1: To write something

Defining foldl in terms of foldr

徘徊边缘 提交于 2019-12-01 20:57:05
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 am currently reading a book on Haskell. And in it, it wrote its own version of the foldl function, but in terms of foldr. I do not follow. Why are there 4 arguments for foldr? What does the id function do? The thing will be become obvious when to expand the expression of foldr step id xs z : As Adam Smith said in the comments: foldr step id xs z = (foldr step id xs) z Consider foldr step id xs firstly foldr step id xs = x1 `step` (foldr step id xs1) = x1 `step` (x2 `step` (foldr step id

第十章 Scala 容器基础(二十):使用reduce和fold方法遍历集合的所有元素

拟墨画扇 提交于 2019-12-01 19:44:36
Problem 你想要遍历有序集合的所有元素,并且随着你对集合元素的遍历,对比两个相邻的元素 Solution 使用 reduceLeft, foldLeft, reduceRight, foldRight来遍历集合的元素,你的方法作用在相邻的两个元素上,从第一次要遍历的两个相邻元素开始,把你的方法作用在这两个元素上得到返回值,然后把你的方法继续作用在返回值和集合中第三要遍历的元素得到的返回值,再继续和第四个要遍历的元素作用。。。直到遍历完最后一个元素为止: scala> val a = Array(12, 6, 15, 2, 20, 9) a: Array[Int] = Array(12, 6, 15, 2, 20, 9) scala> a.reduceLeft(_ + _) res32: Int = 64 这个例子是这样的:12+6=18, 18+15=33, 33+2=35, 35+20=55, 55+9=64,就是对集合的所有元素求和 接下来你会看到如何使用reduceLeft来计算集合元素的乘积和求最大最小值: scala> a.reduceLeft(_ * _) res33: Int = 388800 scala> a.reduceLeft(_ min _) res34: Int = 2 scala> a.reduceLeft(_ max _) res35: Int =

Haskell/GHC performance of `any`/`all`

╄→гoц情女王★ 提交于 2019-12-01 19:32:55
I wrote quantification functions exists , forall , and none for Haskell's build-in [] list data type. On multiple occasions, these seemed to prove much more efficient than Prelude / Data.List s any and all . I naively suspect that this performance is due to any and all being implemented using Θ(n) folds. Since I am relatively new to Haskell, I think I must be mistaken, or that there would be a good reason for this phenomenon. From Data.Foldable : -- | Determines whether any element of the structure satisfies the predicate. any :: Foldable t => (a -> Bool) -> t a -> Bool any p = getAny #.

Are fold expressions subject to short-circuiting?

守給你的承諾、 提交于 2019-12-01 19:00:55
In C++17, are fold expressions subject to short-circuiting when used with && or || as their operator? If so, where is this specified? 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 op ( ... op (E_N_minus_1 op E_N)) for a unary right fold, (((E op E_1) op E_2) op ... ) op E_N for a binary

Reverse a list in Scheme with foldl and foldr

痴心易碎 提交于 2019-12-01 18:49:30
How can you define a function to reverse a list in Scheme by using foldr and foldl ? What we want is a succinct solution to reverse a list in Scheme using a foldl call and a different solution using a foldr call, as defined: (define (foldl operation lst initial) (if (null? lst) initial (foldl operation (cdr lst) (operation (car lst) initial)))) and (define (foldr operation lst initial) (if (null? lst) initial (operation (car lst) (foldr operation (cdr lst) initial)))) The astute person will observe that the foldl implementation is tail-recursive because the returned value is computed as each

Haskell recursive function example with foldr

拜拜、爱过 提交于 2019-12-01 17:46:36
I've taken up learning Haskell again, after a short hiatus and I am currently trying to get a better understanding of how recursion and lambda expressions work in Haskell. In this: YouTube video , there is a function example that puzzles me far more than it probably should, in terms of how it actually works: firstThat :: (a -> Bool) -> a -> [a] -> a firstThat f = foldr (\x acc -> if f x then x else acc) For the sake of clarity and since it wasn't immediately obvious to me, I'll give an example of applying this function to some arguments: firstThat (>10) 2000 [10,20,30,40] --returns 20, but