Pau*_*ar. 0 haskell functional-programming
最后三行产生错误.如何在不出错的情况下获取这些表达式的类型?谢谢,麻烦您了.
--
-- pairs of Int
--
data Pair = P Int Int deriving (Show, Eq)
pairFst (P x y ) = x
pairSnd (P x y ) = y
instance Ord Pair where
compare (P x1 y1) (P x2 y2) =
case compare x1 x2 of EQ -> compare y1 y2
LT -> LT
GT -> GT
--
-- pairs of things of type a
--
data Pair a = P a a deriving (Show, Eq)
pairFst (P x y ) = x
pairSnd (P x y ) = y
-- ordering on same kinds of pairs pairs of the same type of thing
instance Ord a => Ord (Pair a) where
compare (P x1 y1) (P x2 y2) =
case compare x1 x2 of EQ -> compare y1 y2
LT -> LT
GT -> GT
--
-- pairs of things of type a and b
--
data Pair a b = P a b deriving (Show, Eq)
pairFst (P x y ) = x
pairSnd (P x y ) = y
-- ordering on same kinds of pairs pairs of the same type of thing
instance (Ord a, Ord b) => Ord (Pair a b) where
compare (P x1 y1) (P x2 y2) =
case compare x1 x2 of EQ -> compare y1 y2
LT -> LT
GT -> GT
--
-- pairs of things of type a and b, automatic deriving
--
data Pair a b = P a b deriving (Show, Eq, Ord)
pairFst (P x y ) = x
pairSnd (P x y ) = y
:t (<) P 1 2
:t (<) P 1 'a'
:t (<) P 1 "a"
Run Code Online (Sandbox Code Playgroud)
您无法让编译器本身在编译时轻松地显示类型,并且类型已在运行时被擦除.相反,使用GHCi.:t从文件中删除有问题的行,然后运行
$ ghci WhateverFile.hs
*> :t (<) P 1 2
...
Run Code Online (Sandbox Code Playgroud)
作为一个提示,答案是"没什么,这是一个类型错误",因为你忘记了parens.我想你想要的
(<) (P 1 2)
Run Code Online (Sandbox Code Playgroud)
哪个应该有类型
(Num a, Num b) => P a b -> Bool
Run Code Online (Sandbox Code Playgroud)
或者它的一些不太通用的版本.