【发布时间】:2015-01-17 16:30:36
【问题描述】:
我在查看一些代码时遇到了以下 gem,我敢打赌它是 pointfree 输出的复制粘贴:
(对于这个特定问题,我认为以下内容比通常的 foo/bar 更合适:P)
import Control.Monad (liftM2)
data Battleship = Battleship { x :: Int
, y :: Int
} deriving Show
placeBattleship :: Int -> Int -> Battleship
placeBattleship x' y' = Battleship { x = x', y = y' }
coordinates :: Battleship -> (Int, Int)
coordinates = liftM2 (,) x y
有人能解释一下从:
(i) coordinates b = (x b, y b)
到:
(ii) coordinates = liftM2 (,) x y 所需的步骤吗?
特别是,我对 liftM2 的使用感到有些困惑,因为我什至不知道背后潜伏着一个 monad。
我知道 (i) 也可以表示为:coordinates s = (,) (x s) (y s),但我不确定从哪里/如何继续。
附:以下是我怀疑它来自pointfree 的原因(输出来自GHCI 和:pl 别名为pointfree):
λ: :pl coordinates s = (x s, y s)
coordinates = liftM2 (,) x y
【问题讨论】: