foldleft

Scala fold right and fold left

≯℡__Kan透↙ 提交于 2020-06-10 03:29:48
问题 I am trying to learn functional programming and Scala, so I'm reading the "Functional Programming in Scala" by Chiusano and Bjarnason. I' m having trouble understanding what fold left and fold right methods do in case of a list. I've looked around here but I haven't find something beginner friendly. So the code provided by the book is: def foldRight[A,B](as: List[A], z: B)(f: (A, B) => B): B = as match { case Nil => z case Cons(h, t) => f(h, foldRight(t, z)(f)) } def foldLeft[A,B](l: List[A],

How to sum number of Ints and Number of Floats within a List - Scala

笑着哭i 提交于 2020-05-15 07:40:30
问题 I need to calculate the number of integers and floats i have in a Map which is like Map[String, List[(Int, String, Float)]] The data comes from reading a file - the data inside for example looks kinda like (however there is a few more Routes): Cycle Route (City),1:City Centre :0.75f,2:Main Park :3.8f,3:Central Station:2.7f,4:Modern Art Museum,5:Garden Centre:2.4f,6:Music Centre:3.4f The map is split so that the String is the name of the route and the List is the rest of the data. I want it to

How to make this Scheme function not tail recursive?

南楼画角 提交于 2020-03-04 21:27:21
问题 I can't figure out how can I make this tail recursive Scheme function not tail recursive anymore. Anyone can help me? (define (foldrecl f x u) (if (null? x) u (foldrecl f (cdr x) (f (car x) u)))) 回答1: left folds are inheritly iterative, but you can easily make them recursive by adding a continuation. eg. (let ((value expresion-that-calculates)) value) So in your case: (define (foldrecl f x u) (if (null? x) u (let ((result (foldrecl f (cdr x) (f (car x) u)))) result))) While this looks

Scala: Value :: is not a member of Int

天涯浪子 提交于 2020-01-05 09:25:14
问题 I have following Scala code sample and I want to know why I get an Error on foldLeft but not with foldRight? val xs = List(1,2,3) val ys = List(4,5,6) (xs foldLeft ys) (_::_) // Error: Value :: is not a member of Int (xs foldRight ys) (_::_) // Res: List(1, 2, 3, 4, 5, 6) I am new to Scala, so please reply as simple as you can. Thanks 回答1: The arguments for the operator (function) passed to foldLeft and foldRight are in opposite order. So in foldLeft your _ :: _ starts with ys :: xs.head ,

Scala: How do I use foldLeft with a generic array?

折月煮酒 提交于 2019-12-23 02:22:51
问题 I've this method: def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean = { val sorted = for (it <- as.sliding(2)) yield { if (it.length == 2) ordered.apply(it(0), it(1)) else true } sorted.find(!_).isEmpty } What I'd like to do is use foldLeft and apply the binary operator. However, foldLeft requires an initial value and I don't know what initial value I can provide without knowing the real type of A . 回答1: I think what you're doing can be simplified. def isSorted[A](as: Array[A

Scala: How do I use foldLeft with a generic array?

痴心易碎 提交于 2019-12-06 14:53:32
I've this method: def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean = { val sorted = for (it <- as.sliding(2)) yield { if (it.length == 2) ordered.apply(it(0), it(1)) else true } sorted.find(!_).isEmpty } What I'd like to do is use foldLeft and apply the binary operator. However, foldLeft requires an initial value and I don't know what initial value I can provide without knowing the real type of A . I think what you're doing can be simplified. def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean = { if (as.size < 2) true else as.sliding(2).find(x => !ordered(x(0)

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 :

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

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

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