fold

Implementation of function “member” using “foldr” in Haskell

喜你入骨 提交于 2021-02-16 14:02:44
问题 I tried like that: member e [] = False member e xs = foldr (==) e xs and then: member 3 [1,2,3,4,5] and I get this error message: No instance for (Num Bool) arising from the literal `3' In the first argument of `memb', namely `3' I don't know what it means... can someone help me? PS: member is a function that, given a element and a list of elements, returns whether the element belongs to that list or not. The usual implementation is: member a [] = False member a (x:xs) | (a == x) = True |

Walk through a list split function in Haskell

血红的双手。 提交于 2021-02-08 14:11:47
问题 This is a follow up to my previous question. I am trying to understand the list splitting example in Haskell from here: foldr (\a ~(x,y) -> (a:y,x)) ([],[]) I can read Haskell and know what foldr is but don't understand this code. Could you walk me through this code and explain it in more details ? 回答1: Let’s try running this function on a sample input list, say [1,2,3,4,5] : We start with foldr (\a ~(x,y) -> (a:y,x)) ([],[]) [1,2,3,4,5] . Here a is the first element of the list, and (x,y)

Practical use of fold/reduce in functional languages

喜欢而已 提交于 2021-02-07 12:16:31
问题 Fold (aka reduce ) is considered a very important higher order function. Map can be expressed in terms of fold (see here). But it sounds more academical than practical to me. A typical use could be to get the sum, or product, or maximum of numbers, but these functions usually accept any number of arguments. So why write (fold + 0 '(2 3 5)) when (+ 2 3 5) works fine. My question is, in what situation is it easiest or most natural to use fold ? 回答1: The point of fold is that it's more abstract.

Practical use of fold/reduce in functional languages

无人久伴 提交于 2021-02-07 12:15:39
问题 Fold (aka reduce ) is considered a very important higher order function. Map can be expressed in terms of fold (see here). But it sounds more academical than practical to me. A typical use could be to get the sum, or product, or maximum of numbers, but these functions usually accept any number of arguments. So why write (fold + 0 '(2 3 5)) when (+ 2 3 5) works fine. My question is, in what situation is it easiest or most natural to use fold ? 回答1: The point of fold is that it's more abstract.

Haskell - creating a function definition for the function `all` using foldr

旧巷老猫 提交于 2021-02-05 12:30:55
问题 I'm trying to create a function definition for the function all using foldr . p is the predicate. I know this can be done: all p = and . foldr (\x xs -> p x : xs) [] But what I want to do is to shift the function and into the foldr equation. Can this be done? I've tried the following, which all failed to work: all p = foldr (\x p -> \ys -> and (p x) ys) True all p = foldr (\x and -> (\ys -> (p x and ys))) True all p = foldr (\x ys -> and . (p x) ys) True Am I falling short in my understanding

Scala foldLeft while some conditions are true

限于喜欢 提交于 2021-02-04 17:38:05
问题 How to emulate following behavior in Scala? i.e. keep folding while some certain conditions on the accumulator are met. def foldLeftWhile[B](z: B, p: B => Boolean)(op: (B, A) => B): B For example scala> val seq = Seq(1, 2, 3, 4) seq: Seq[Int] = List(1, 2, 3, 4) scala> seq.foldLeftWhile(0, _ < 3) { (acc, e) => acc + e } res0: Int = 1 scala> seq.foldLeftWhile(0, _ < 7) { (acc, e) => acc + e } res1: Int = 6 UPDATES: Based on @Dima answer, I realized that my intention was a little bit side

Is there a way to fold with index in Rust?

一曲冷凌霜 提交于 2021-01-26 05:58:52
问题 In Ruby, if I had an array a = [1, 2, 3, 4, 5] and I wanted to get the sum of each element times its index I could do a.each.with_index.inject(0) {|s,(i,j)| s + i*j} Is there an idiomatic way to do the same thing in Rust? So far, I have a.into_iter().fold(0, |x, i| x + i) But that doesn't account for the index, and I can't really figure out a way to get it to account for the index. Is this possible and if so, how? 回答1: You can chain it with enumerate: fn main() { let a = [1, 2, 3, 4, 5]; let

Powerset of a list using abstract list functions

£可爱£侵袭症+ 提交于 2021-01-04 05:51:25
问题 Is it possible to make a racket function to return powerset of a given list ? Constraints- without explicit recursion use abstract list functions code contained in 2 lines (actual requirement) For eg: (powerset '(1 2)) '((1 2) (1) (2) ()) in any order. The other question I found still uses explicit recursion. My workflow: Taking (powerset '(a b c)) as an example, First get a list of whole numbers up to (expt 2 (length list)) ;'(0 1 2 3 4 5 6 7) Convert them into their respective binary form 2

How to write foldr (right fold) generator in Python?

戏子无情 提交于 2021-01-03 01:09:08
问题 Python's reduce is a left-fold, which means it is tail-recursive and its uses can be neatly rewritten as a loop. However, Python does not have a built-in function for doing right folds. Since right-folds are most naturally written with recursion (and Python doesn't like recursion as much as functional languages), I'm interested in writing a right fold ( foldr ) in terms of a generator. How can this be done? And very specifically, how can it be done in Python 2.7? EDIT: I should have mentioned

How to write foldr (right fold) generator in Python?

吃可爱长大的小学妹 提交于 2021-01-03 00:56:08
问题 Python's reduce is a left-fold, which means it is tail-recursive and its uses can be neatly rewritten as a loop. However, Python does not have a built-in function for doing right folds. Since right-folds are most naturally written with recursion (and Python doesn't like recursion as much as functional languages), I'm interested in writing a right fold ( foldr ) in terms of a generator. How can this be done? And very specifically, how can it be done in Python 2.7? EDIT: I should have mentioned