向右折叠与向左折叠

folding right vs left

这是你结束的地方和你开始的地方之间的 discussed to here on SO, but my specific example is escaping me because I feel that my fold shouldn't care whether it's composed right-to-left or left-to-right. This is a solution to day 1 of 2016's Advent of Code which boils down to taking a list of instructions (turn right/left, walk x steps forward), applying them, and giving the taxicab-geometry 距离。

我写了一个apply函数来处理这个旅程的一个步骤,它有签名:

data Direction   = North | East | South | West deriving (Enum, Show)
type Location    = (Int, Int)
type Instruction = String

apply :: Direction -> Location -> Instruction -> (Direction, Location)

暂时假设它已正确实施(因为我对其进行了测试并且确实如此。我将在首屏下方包含一个可运行的示例)。我注意到我可以使用折叠将其应用于整个指令列表,并且

(_, finalLocation) = foldr f (North, (0, 0)) instructions  -- note the foldr.
  where f = (\ins (d, loc) -> apply d loc ins)

这里使用右关联折叠有效,但给了我错误的答案。当我用 foldl(和 flip f)重新 运行 时,我得到了一个完全不同的答案,adventofcode 接受了这个答案,所以我承认折叠方向肯定是不同的,我只是不不知道为什么这是不同的,因为在我看来我的代码不应该关心折叠发生的方式。

为什么我错了?


module AdventOfCode where

-- split
import Data.List.Split (splitOn)

day1input :: String
day1input = "L4, R2, R4, L5, L3, L1, R4, R5, R1, R3, L3, L2, L2, R5, R1, L1, L2, \
            \R2, R2, L5, R5, R5, L2, R1, R2, L2, L4, L1, R5, R2, R1, R1, L2, L3, \
            \R2, L5, L186, L5, L3, R3, L5, R4, R2, L5, R1, R4, L1, L3, R3, R1, L1, \
            \R4, R2, L1, L4, R5, L1, R50, L4, R3, R78, R4, R2, L4, R3, L4, R4, L1, \
            \R5, L4, R1, L2, R3, L2, R5, R5, L4, L1, L2, R185, L5, R2, R1, L3, R4, \
            \L5, R2, R4, L3, R4, L2, L5, R1, R2, L2, L1, L2, R2, L2, R1, L5, L3, L4, \
            \L3, L4, L2, L5, L5, R2, L3, L4, R4, R4, R5, L4, L2, R4, L5, R3, R1, L1, \
            \R3, L2, R2, R1, R5, L4, R5, L3, R2, R3, R1, R4, L4, R1, R3, L5, L1, L3, \
            \R2, R1, R4, L4, R3, L3, R3, R2, L3, L3, R4, L2, R4, L3, L4, R5, R1, L1, \
            \R5, R3, R1, R3, R4, L1, R4, R3, R1, L5, L5, L4, R4, R3, L2, R1, R5, L3, \
            \R4, R5, L4, L5, R2"

day1Processed :: [String]
day1Processed = splitOn ", " day1input

data Direction = North | East | South | West deriving (Enum, Show)
type Location = (Int, Int)

-- |'apply' takes your current 'Direction' and 'Location', applies the instruction
-- and gives back a tuple of (newDirection, (new, location))
apply :: Direction -> Location -> String -> (Direction, Location)
apply d' loc (t:num') = (d, step d loc numsteps)
  where d        = turn d' t
        numsteps = read num' :: Int

-- |'distanceBetween' returns the taxicab geometric distance between two 'Location's
distanceBetween :: Location -> Location -> Int
distanceBetween (x1, y1) (x2, y2) = (abs $ x1-x2) + (abs $ y1-y2)


-- |'turn' changes direction based on the received Char
turn :: Direction -> Char -> Direction
turn West  'R' = North
turn North 'L' = West
turn d     'R' = succ d
turn d     'L' = pred d
turn d      _  = d

-- |'step' moves location based on current direction and number of steps
step :: Direction -> Location -> Int -> Location
step North (x, y) s = (x  , y+s)
step East  (x, y) s = (x+s, y)
step South (x, y) s = (x  , y-s)
step West  (x, y) s = (x-s, y)

wrongLocation :: Location
rightLocation :: Location
(_, wrongLocation) = foldr (\x (d, loc) -> apply d loc x) (North, (0, 0)) day1Processed
(_, rightLocation) = foldl (\(d, loc) x -> apply d loc x) (North, (0, 0)) day1Processed

wrongAnswer :: Int
rightAnswer :: Int
wrongAnswer = distanceBetween (0, 0) wrongLocation
rightAnswer = distanceBetween (0, 0) rightLocation

根据评论,我认为您对 foldlfoldr 之间的区别有些困惑。我将尝试在这里区分它们。让我们看一个最小的例子。

foldr f x [a, b, c] = a `f` (b `f` (c `f` x))
foldl g x [a, b, c] = ((x `g` a) `g` b) `g` c

这就是这些函数在包含三个元素的较小列表上展开的方式。现在,让我们假设 g = flip f 然后看看 foldl 做了什么。

foldr f x [a, b, c] = a `f` (b `f` (c `f` x))
foldl (flip f) x [a, b, c] = c `f` (b `f` (a `f` x))

因此,从某种意义上说,当您执行 foldl (flip f) 而不是 foldr f.

时,列表的顺序最终会颠倒过来

因此,您最初关于 foldl (flip f) === foldr f 的断言通常是错误的,但我们确实以一个相当有趣的 属性 代替它。假设我们正在处理的列表是有限的,似乎 foldl (flip f) x === foldr f x . reverse