[<<][compsci][>>][..]
Sat Apr 7 15:18:50 CEST 2012

Left/Right confusion in mapAccum

Basically, I used mapAccumR and my datastructure was reversed.  This
is kind of strange, and it doesn't seem to be explained in the docs.
Have to find the source later.

But the basic idea is that "left" and "right" refer to the order in
which the list is traversed.  The result list is always the same
order.

  mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
  mapAccumR :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])

  *Main> mapAccumL (\acc x -> (acc, x)) 0 [1,2,3]
  (0,[1,2,3])
  *Main> mapAccumR (\acc x -> (acc, x)) 0 [1,2,3]
  (0,[1,2,3])
  *Main>

  *Main> mapAccumR (\l e -> ((e:l),e)) [] [1,2,3]
  ([1,2,3],[1,2,3])
  *Main> mapAccumL (\l e -> ((e:l),e)) [] [1,2,3]
  ([3,2,1],[1,2,3])

From the last example it's clear that L starts at the beginning while
R starts at the end.  So as long as the operator is associative, the
result shouldn't matter.



[Reply][About]
[<<][compsci][>>][..]