Verifying foldl implementation in terms of foldr

I want to verify following implementation of foldl in terms foldr is correct: foldl4 = foldr . flip I used following tests in HUGS: foldl4 (+) 3 [] foldl4 (+) 3 [1,2,3] They worked. Please suggest any more tests I could do. Thanks here is a simple test: foldl (flip (:)) [] should be reverse ... if you want to test foldr vs foldl you probably should not use commutative operations ;)

用foldr验证foldl的实现

我想用foldr来验证foldl是否正确: foldl4 = foldr . flip 我在HUGS中使用了以下测试: foldl4 (+) 3 [] foldl4 (+) 3 [1,2,3] 他们工作。 请提出更多我可以做的测试。 谢谢 这里是一个简单的测试: foldl (flip (:)) []应该是reverse ... 如果你想测试foldr vs foldl你可能不应该使用交换操作;) 这里有一些直接来自GHCi的证据: λ> foldl (flip (:)) [] [1..5] [5,4,3,2,1] λ> foldl4 (flip (:)) [] [1..

Haskell, Foldr, and foldl

I've been trying to wrap my head around foldr and foldl for quite some time, and I've decided the following question should settle it for me. Suppose you pass the following list [1,2,3] into the following four functions: a = foldl (xs y -> 10*xs -y) 0 b = foldl (xs y -> y - 10 * xs) 0 c = foldr (y xs -> y - 10 * xs) 0 d = foldr (y xs -> 10 * xs -y) 0 The results will be -12

Haskell,Foldr和foldl

我一直试图把我的头绕在foldr上并折叠很长时间,而且我已经决定了下面的问题应该为我解决。 假设您将以下列表[1,2,3]传递给以下四个函数: a = foldl (xs y -> 10*xs -y) 0 b = foldl (xs y -> y - 10 * xs) 0 c = foldr (y xs -> y - 10 * xs) 0 d = foldr (y xs -> 10 * xs -y) 0 结果将分别为-123,83,281和-321。 为什么会这样? 我知道,当你将[1,2,3,4]传递给定义为的函数时 f = foldl (xs x -> xs ++

Numerical issue with `foldl` and `foldr` in Haskell

I have the following Haskell script which computes the function f(x) = (2- x) - (2^3 - x^3/12) calc x = (x - (x ^ 3) / 12) calc2 x = (calc 2) - (calc x) calcList1 :: [Float] -> Float calcList1 l = foldl (+) 0.0 (map calc2 l) calcList2 :: [Float] -> Float calcList2 l = foldr (+) 0.0 (map calc2 l) test1 :: Float -> Float test1 step = (calcList1 l) - (calcList2 l) where l

在Haskell中`foldl`和`fol​​dr`的数值问题

我有以下Haskell脚本来计算函数f(x) = (2- x) - (2^3 - x^3/12) calc x = (x - (x ^ 3) / 12) calc2 x = (calc 2) - (calc x) calcList1 :: [Float] -> Float calcList1 l = foldl (+) 0.0 (map calc2 l) calcList2 :: [Float] -> Float calcList2 l = foldr (+) 0.0 (map calc2 l) test1 :: Float -> Float test1 step = (calcList1 l) - (calcList2 l) where l = [0.0,step..2.0] 函数calcList1和

Combining foldl and foldr

I've figured out myself that foldl (or foldl' ) is the best approach when you want to produce summarise a list into one result (ie sum ), and foldr is the best approach when you want to produce another (perhaps even infinite) list (ie filter ). So I was considering was processing that combines these two. So I made the function sum_f . sum_f is fairly simple, all it does is add up the

结合foldl和foldr

我已经弄清楚自己foldl (或foldl' )是最好的方法,当你想产生一个列表到一个结果(即sum ),而foldr是最好的方法,当你想产生另一个(甚至可能是无限的)列表(即filter )。 所以我正在考虑将这两者结合起来进行处理。 所以我做了sum_f函数。 sum_f非常简单,它所做的就是将列表中的元素相加,但如果它找到一个使得fx为真的元素,它将当前结果作为列表元素的输出并从该点开始总结。 代码在这里: sum_f :: (Num a)

foldl and foldr?

Is difference between foldl and foldr just the direction of looping? I thought there was a difference in what they did, not just in the direction? There's a difference if your function isn't associative (ie it matters which way you bracket expressions) so for example, foldr (-) 0 [1..10] = -5 but foldl (-) 0 [1..10] = -55 . On a small scale, this is because 10-(20-(30)) isn't t

foldl和foldr?

foldl和foldr之间的区别只是循环的方向? 我认为他们所做的事情有所不同,而不仅仅是方向? 例如,如果你的函数不是关联的(例如,你括括表达式的方式很重要) foldr (-) 0 [1..10] = -5但foldl (-) 0 [1..10] = -55 。 在小范围内,这是因为10-(20-(30))与((10)-20)-30不相同。 鉴于(+)是关联的(无论您添加子表达式的顺序如何), foldr (+) 0 [1..10] = 55 , foldl (+) 0 [1..10] = 55 。 (++)是另一个关联操作

foldl / foldr query

I'm a beginner at Haskell, and even after reading several explanations of foldr/foldl, I can't understand why I'm getting different results below. What is the explanation? Prelude> foldl (_ -> (+1)) 0 [1,2,3] 4 Prelude> foldr (_ -> (+1)) 0 [1,2,3] 3 Thanks! In the foldl case, the lambda is being passed the accumulator as the first argument, and the list element as the

foldl / foldr查询

我是Haskell的初学者,甚至在阅读了foldr / foldl的几个解释之后,我无法理解为什么我会在下面得到不同的结果。 什么是解释? Prelude> foldl (_ -> (+1)) 0 [1,2,3] 4 Prelude> foldr (_ -> (+1)) 0 [1,2,3] 3 谢谢! 在foldl情况下,lambda传递的是累加器作为第一个参数,而列表元素作为第二个参数。 在foldr情况下,lambda被作为第一个参数传递给list元素,第二个作为累加器。 你的lambda忽略了第一个参

foldl versus foldr behavior with infinite lists

The code for the myAny function in this question uses foldr. It stops processing an infinite list when the predicate is satisfied. I rewrote it using foldl: myAny :: (a -> Bool) -> [a] -> Bool myAny p list = foldl step False list where step acc item = p item || acc (Note that the arguments to the step function are correctly reversed.) However, it no longer stops processing infinite

foldl与foldr行为与无限列表

此问题中myAny函数的代码使用foldr。 当谓词满足时,它停止处理无限列表。 我用foldl重写了它: myAny :: (a -> Bool) -> [a] -> Bool myAny p list = foldl step False list where step acc item = p item || acc (注意step函数的参数正确颠倒了。) 但是,它不再停止处理无限列表。 我试图跟踪Apocalisp答案中的函数执行情况: myAny even [1..] foldl step False [1..] step (foldl step False [2..]) 1 ev

sum of squares of integers

This question already has an answer here: Sum of Squares using Haskell 3 answers From what I understand, you want to take two numbers, eg 1 and 10 , square each number between them (inclusively), and then take the sum of that. So you'd want some function like sumOfSquaresBetween :: Int -> Int -> Int sumOfSquaresBetween m n = ??? Now, you have to use recursion, so this means that

整数平方和

这个问题在这里已经有了答案: 使用Haskell 3回答的正方形总和 根据我的理解,你想在它们之间取两个数字,例如1和10 ,每个数字(包含),然后取这两个数字的总和。 所以你会想要一些功能 sumOfSquaresBetween :: Int -> Int -> Int sumOfSquaresBetween m n = ??? 现在,你必须使用递归,所以这意味着??? 将会是一些使用sumOfSquaresBetween表达式。 现在有个技巧:如果你知道sumOfSquares nn ,那么你将如何找

Are these functions tail recursive?

I'm learning tail recursion and I'm having some difficulties in determining whether my function is tail recursive or not (mainly on functions which I use another function). I've implemented the two following functions but I'm not so sure if they are tail recursive or not. The first one is a function to concatenate two lists. conca list [] = list conca [] result = result conca

这些函数是否递归?

我正在学习尾递归,并且在确定我的函数是否是递归的尾部方面遇到了一些困难(主要是我使用另一个函数的函数)。 我已经实现了以下两个函数,但我不确定它们是否是递归的尾部。 第一个是连接两个列表的函数。 conca list [] = list conca [] result = result conca (h:t) result = conca (init (h:t)) ( last(h:t):result ) concatenate::[a]->[a]->[a] concatenate list1 list2 = conca list1 list2 函数中的计算

Tail Recursion in Haskell

I am trying to understand tail-recursion in Haskell. I think I understand what it is and how it works but I'd like to make sure I am not messing things up. Here is the "standard" factorial definition: factorial 1 = 1 factorial k = k * factorial (k-1) When running, for example, factorial 3 , my function will call itself 3 times(give it or take). This might pose a problem if I w

在Haskell中的尾递归

我想了解Haskell中的尾递归。 我想我明白它是什么,它是如何工作的,但我想确保我不会搞砸。 这是“标准”阶乘定义: factorial 1 = 1 factorial k = k * factorial (k-1) 例如,运行时, factorial 3 ,我的功能将自己调用3次(给它或拿)。 如果我想计算阶乘99999999,这可能会造成问题,因为我可能有堆栈溢出。 在达到factorial 1 = 1我将不得不“堆栈”并乘以所有值,因此我有6个操作(3个用于调用函数本身,3个用于乘数