Problem with function Enum.foldr

Hello people, it is my first post on the forum, and i would like some help to understand something that seems wrong with the function Enum.foldr, as it returned the same result of Enum.foldl, which i wasn’t expecting.

I was following one of the examples of the book “Introducing Elixir - Simon St. Laurent” where it explains how both functions works and even give an example where these functions returns different results. But when i tried the same test on iex, it returned the same result for both. See below:

list = [2, 4, 8, 16, 32]
divide = fn(value, accumulator) -> value / accumulator end
Enum.foldl(list, 1, divide) #Returns 8.0, which is correct
Enum.foldr(list, 1, divide) #Returns 8.0, when, according to the book, should be 0.125

Neither Enum.foldl or Enum.foldr are functions, do you mean List.foldr/l ?

In any case it would seem that the example in that book is wrong then. If you work through the math you get 8 in both cases. For foldr you’ve got:

32 / 1 => 32
16 / 32 => 0.5
8 / 0.5 => 16
4 / 16 => 0.25
2 / 0.25 => 8
3 Likes

you are right, I meant List.foldr/1.

I´ve already found the problem. It turns out that the list was [1, 2, 4, 8, 16, 32]. Very basic mistake. Sorry and thanks for the fast reply

1 Like