-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Exercise: Python β "List Ops" foldr implementation
Issue Type: Test case inconsistency
β Problem
The test case in the foldr exercise appears to use an argument order suited for foldl, not foldr. Specifically:
self.assertEqual(foldr(lambda acc, el: el / acc, [1, 2, 3, 4], 24), 9)This test passes only if the foldr implementation applies the function as func(acc, el), which matches foldl semantics.
However, standard foldr behavior is: func(head, foldr(func, tail, acc))
which expects a function with signature lambda el, acc:
π Example
A proper right fold would expect:
foldr(lambda el, acc: el / acc, [1, 2, 3, 4], 24)
Evaluates as: 1 / (2 / (3 / (4 / 24))) β 9
The current test:
foldr(lambda acc, el: el / acc, [1, 2, 3, 4], 24)
...only produces the same result if the implementation applies the lambda backward, i.e., as func(foldr(...), head), which is incorrect for a proper foldr.
π¬ Suggested Fixes
β
Fix the lambda in the test case to use correct argument order:
lambda el, acc: el / acc
β Or clarify in the test description that the fold direction or argument order is intentionally reversed (though that would contradict conventional foldr behavior).
π Why This Matters
This caused significant confusion and wasted hours of debugging, thinking the recursion was broken, when in fact the test violates the expected function signature for a right fold.
I saw the note saying the ordering of arguments is significant, and I agree β but the provided test still uses a lambda with argument order suited for foldl, while the function being tested is foldr. That contradiction caused confusion and should be corrected or clarified. Clarifying or correcting this will help future learners stay focused on recursion and functional logic, instead of stumbling over a mismatched test.
I appreciate your work in providing a platform that helps learners like me explore these concepts, and am now able to progress further.