I was reviewing some code and came across the following gem, which I'd wager is a copy-paste of pointfree
output:
(I thought the following would more appropriate than the usual foo
/bar
for this particular question :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
Would someone be kind enough to explain the steps needed to simplify from:
(i) coordinates b = (x b, y b)
to:
(ii) coordinates = liftM2 (,) x y
?
In particular, I'm a bit confused as to the use of liftM2
as I wasn't even aware that a monad was lurking in the background.
I know that (i) can also be represented as: coordinates s = (,) (x s) (y s)
but I'm not sure where/how to proceed.
P.S. The following is why I suspect it's from pointfree
(output is from GHCI
and :pl
is aliased to pointfree
):
λ: :pl coordinates s = (x s, y s)
coordinates = liftM2 (,) x y