Haskell 在GHCi中使用let部分应用fst和snd会给出一个奇怪的类型签名
我正在实现一个相当普通的二叉树,我正在实现一个Haskell 在GHCi中使用let部分应用fst和snd会给出一个奇怪的类型签名,haskell,binary-tree,ghci,Haskell,Binary Tree,Ghci,我正在实现一个相当普通的二叉树,我正在实现一个insertWith函数,这样您就可以通过将现有值转换为它们的“键”来插入值——这可以让您存储(键,值)的元组,并在插入新的“节点”时仅比较键。实施情况如下: insertWith :: (Ord b) => (a -> b) -> BinTree a -> a -> BinTree a insertWith _ EmptyTree y = Node y EmptyTree EmptyTree insertWith
insertWith
函数,这样您就可以通过将现有值转换为它们的“键”来插入值——这可以让您存储(键,值)的元组,并在插入新的“节点”时仅比较键。实施情况如下:
insertWith :: (Ord b) => (a -> b) -> BinTree a -> a -> BinTree a
insertWith _ EmptyTree y = Node y EmptyTree EmptyTree
insertWith f (Node x l r) y =
case compare (f y) (f x) of
LT -> Node x (insertWith f l y) r
EQ -> Node x l r
GT -> Node x l (insertWith f r y)
let insertWith_ = insertWith fst
:t insertWith_
insertWith_ :: BinTree ((), b) -> ((), b) -> BinTree ((), b)
:t insertWith fst
insertWith fst
:: (Ord a) => BinTree (a, b) -> (a, b) -> BinTree (a, b)
当我在GHCi中使用let
部分应用fst
时,我得到以下结果:
insertWith :: (Ord b) => (a -> b) -> BinTree a -> a -> BinTree a
insertWith _ EmptyTree y = Node y EmptyTree EmptyTree
insertWith f (Node x l r) y =
case compare (f y) (f x) of
LT -> Node x (insertWith f l y) r
EQ -> Node x l r
GT -> Node x l (insertWith f r y)
let insertWith_ = insertWith fst
:t insertWith_
insertWith_ :: BinTree ((), b) -> ((), b) -> BinTree ((), b)
:t insertWith fst
insertWith fst
:: (Ord a) => BinTree (a, b) -> (a, b) -> BinTree (a, b)
但是,省略let
步骤会得到以下结果:
insertWith :: (Ord b) => (a -> b) -> BinTree a -> a -> BinTree a
insertWith _ EmptyTree y = Node y EmptyTree EmptyTree
insertWith f (Node x l r) y =
case compare (f y) (f x) of
LT -> Node x (insertWith f l y) r
EQ -> Node x l r
GT -> Node x l (insertWith f r y)
let insertWith_ = insertWith fst
:t insertWith_
insertWith_ :: BinTree ((), b) -> ((), b) -> BinTree ((), b)
:t insertWith fst
insertWith fst
:: (Ord a) => BinTree (a, b) -> (a, b) -> BinTree (a, b)
我认为这与类型签名中的(Ord b)有关,但我只是想知道为什么GHCi在使用let时会这样转换类型签名?提前感谢您的回答。可怕的单态性限制……正是我想要的,谢谢:-)我应该说,MR与ghci的长期违约相结合。