我正在查看一些代码并遇到以下 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)
但我不确定在哪里/如何进行。
PS以下是我怀疑它来自pointfree
(输出来自GHCI
并:pl
别名为pointfree
)的原因:
λ: :pl coordinates s = (x s, y s)
coordinates = liftM2 (,) x y