我想知道为什么下面是对类型的正确实现。具体来说,为什么Pair b a而不是Pair a b
newtype Pair b a = Pair { getPair :: (a,b) } 澄清一下,Pair a b不适用于以下方面:
instance Functor (Pair c) where  
fmap f (Pair (x,y)) = Pair (f x, y) 我不明白为什么。
除了下面的许多很好的答案之外,我发现在ghci中做以下工作很有帮助:
*Main> newtype Pair b a = Pair (a, b) deriving (Show, Eq)
*Main> :t Pair(True, "cat")
Pair(True, "cat") :: Pair [Char] Bool
*Main> newtype Pair a b = Pair (a, b) deriving (Show, Eq)
*Main> :t Pair(True, "cat")
Pair(True, "cat") :: Pair Bool [Char]发布于 2015-04-27 12:52:06
newtype Pair a b和newtype Pair b a都是正确的(就像它们键入check一样)。在后一种情况下,第二种元素的类型是第一种,这有悖于直觉,尽管它在用例中可能更合适。
https://stackoverflow.com/questions/29896327
复制相似问题