Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 在GHCi中使用let部分应用fst和snd会给出一个奇怪的类型签名_Haskell_Binary Tree_Ghci - Fatal编程技术网

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的长期违约相结合。