Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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 哈斯克尔:压缩树的问题_Haskell_Tree_Ghci - Fatal编程技术网

Haskell 哈斯克尔:压缩树的问题

Haskell 哈斯克尔:压缩树的问题,haskell,tree,ghci,Haskell,Tree,Ghci,我是Haskell的新手,一直在尝试构建一个zipping函数,该函数用于具有以下数据结构的树: data Tree a = Leaf | Node a (Tree a) (Tree a) deriving Show 到目前为止,我有: treezip :: (Tree a) -> (Tree b) -> (Tree(a,b)) treezip (Node a leftSubtreea rightSubtreea) (Node b leftSubtreeb rightSubtree

我是Haskell的新手,一直在尝试构建一个zipping函数,该函数用于具有以下数据结构的树:

data Tree a = Leaf | Node a (Tree a) (Tree a) deriving Show
到目前为止,我有:

treezip :: (Tree a) -> (Tree b) -> (Tree(a,b))
treezip (Node a leftSubtreea rightSubtreea) (Node b leftSubtreeb rightSubtreeb) =
let l = treezip leftSubtreea leftSubtreeb
    r = treezip rightSubtreea rightSubtreeb
in Node a l r
但是,每当我尝试将模块加载到GHCi时,我都会收到错误,指向最后一行代码,特别是变量
a


我一直绞尽脑汁想弄明白为什么这行不通。任何帮助都将不胜感激

不要与编译器对抗,让它帮助你吧

要更正这样的函数,最好从“外壳定义”开始:

treeZip (Node a lSa rSa) (Node b lSb rSb) = Node _ _ _
编译器将返回以下内容:

• Found hole: _ :: (a, b)
  Where: ‘a’ is a rigid type variable ...(_bla, bla_)
         ‘b’ is a rigid type variable ....
• In the first argument of ‘Node’, namely ‘_’
  In the expression: Node _ _ _
  In an equation for ‘treeZip’:
      treeZip (Node a lSa rSa) (Node b lSb rSb) = Node _ _ _
• Relevant bindings include
    rSb :: Tree b (bound at /tmp/wtmpf-file20584.hs:4:38)
    lSb :: Tree b (bound at /tmp/wtmpf-file20584.hs:4:34)
    b :: b (bound at /tmp/wtmpf-file20584.hs:4:32)
    rSa :: Tree a (bound at /tmp/wtmpf-file20584.hs:4:21)
    lSa :: Tree a (bound at /tmp/wtmpf-file20584.hs:4:17)
    a :: a (bound at /tmp/wtmpf-file20584.hs:4:15)
因此,它告诉您第一个
类型的孔应该用
(a,b)
类型的东西填充。我们有那种类型的吗?好吧,我们可以很容易地构建它,也就是说,把
a
b
放在一个元组中

treezip (Node a lSa rSa) (Node b lSb rSb) = Node (a,b) _ _
…给予

• Found hole: _ :: Tree (a, b)
  Where: ‘a’ is a rigid type variable bound by...
是的,你已经解决了这个问题——它应该是子树的拉链

treeZip (Node a lSa rSa) (Node b lSb rSb)
     = Node (a,b) (treeZip lSa lSb) (treeZip rSa rSb)

不要反对编译器,让它帮助你吧

要更正这样的函数,最好从“外壳定义”开始:

treeZip (Node a lSa rSa) (Node b lSb rSb) = Node _ _ _
编译器将返回以下内容:

• Found hole: _ :: (a, b)
  Where: ‘a’ is a rigid type variable ...(_bla, bla_)
         ‘b’ is a rigid type variable ....
• In the first argument of ‘Node’, namely ‘_’
  In the expression: Node _ _ _
  In an equation for ‘treeZip’:
      treeZip (Node a lSa rSa) (Node b lSb rSb) = Node _ _ _
• Relevant bindings include
    rSb :: Tree b (bound at /tmp/wtmpf-file20584.hs:4:38)
    lSb :: Tree b (bound at /tmp/wtmpf-file20584.hs:4:34)
    b :: b (bound at /tmp/wtmpf-file20584.hs:4:32)
    rSa :: Tree a (bound at /tmp/wtmpf-file20584.hs:4:21)
    lSa :: Tree a (bound at /tmp/wtmpf-file20584.hs:4:17)
    a :: a (bound at /tmp/wtmpf-file20584.hs:4:15)
因此,它告诉您第一个
类型的孔应该用
(a,b)
类型的东西填充。我们有那种类型的吗?好吧,我们可以很容易地构建它,也就是说,把
a
b
放在一个元组中

treezip (Node a lSa rSa) (Node b lSb rSb) = Node (a,b) _ _
…给予

• Found hole: _ :: Tree (a, b)
  Where: ‘a’ is a rigid type variable bound by...
是的,你已经解决了这个问题——它应该是子树的拉链

treeZip (Node a lSa rSa) (Node b lSb rSb)
     = Node (a,b) (treeZip lSa lSb) (treeZip rSa rSb)

提示:压缩后
Node
存储的值应该是什么?压缩后
Node
应该存储
Int
值。@J.Doe
Int
来自哪里
treezip
获取两个存储任意数据的树,并生成一个包含数据元组的树。如果我有一个
树Char
值和一个
树Bool
值,我必须生成一个
树(Char,Bool)
值。那么,
treezip(Node'c'Leaf)(Node'True'Leaf)
应该返回什么呢?更糟糕的是,
treezip-Leaf(Node'a'Leaf)
应该返回什么呢?只有形状相同的树才能(显然)压缩,因此函数类型可能应该是
treea->treeb->Maybe(treea(a,b))
。提示:压缩后a
Node
存储的值应该是什么?压缩后a
Node
应该存储
Int
值。@J.Doe
Int
来自哪里
treezip
获取两个存储任意数据的树,并生成一个包含数据元组的树。如果我有一个
树Char
值和一个
树Bool
值,我必须生成一个
树(Char,Bool)
值。那么,
treezip(Node'c'Leaf)(Node'True'Leaf)
应该返回什么呢?更糟糕的是,
treezip-Leaf(Node'a'Leaf)
应该返回什么呢?只有形状相同的树才能(显然)被压缩,因此函数类型应该是
treea->treeb->Maybe(treea(a,b))