Haskell 树木地图
我的树是由
data Tree a = Leaf a | Node (Tree a) (Tree a)
deriving (Show)
我还声明一个测试树定义的。
myTree = Node (Node (Leaf 1) (Leaf 2)) (Leaf 3)
我想要做的是创建一个函数 maptree f 它将作用于 Leaf。更具体地说,fx = x +1
,
那么maptree f myTree
将返回
Node (Node (Leaf 2) (Leaf 3)) (Leaf 4)
我的解决方案是,
maptree f (Leaf a)= Leaf (f a)
maptree f (Node xl xr ) = Node (maptree xl) (maptree xr)
但它会返回以下错误
Couldn't match expected type `Tree a'
against inferred type `Tree t -> Tree t'
Probable cause: `maptree' is applied to too few arguments
In the first argument of `Node', namely `(maptree xl)'
In the expression: Node (maptree xl) (maptree xr)
失败,模块已加载:无。
然而,如果我这样做的话,
maptree (Leaf a)= Leaf ( a + 1)
maptree (Node xl xr ) = Node (maptree xl) (maptree xr)
它确实有效。
我看不出第一个功能和第二个功能之间的区别。我如何得到错误?谢谢。
My tree is defined by
data Tree a = Leaf a | Node (Tree a) (Tree a)
deriving (Show)
I also declare a testing tree.
myTree = Node (Node (Leaf 1) (Leaf 2)) (Leaf 3)
What I want to do is create a function maptree f which will act on Leaf. To be more specifically, f x = x +1
,
then maptree f myTree
will return
Node (Node (Leaf 2) (Leaf 3)) (Leaf 4)
My solution is
maptree f (Leaf a)= Leaf (f a)
maptree f (Node xl xr ) = Node (maptree xl) (maptree xr)
but it will return the following error
Couldn't match expected type `Tree a'
against inferred type `Tree t -> Tree t'
Probable cause: `maptree' is applied to too few arguments
In the first argument of `Node', namely `(maptree xl)'
In the expression: Node (maptree xl) (maptree xr)
Failed, modules loaded: none.
However, if I do
maptree (Leaf a)= Leaf ( a + 1)
maptree (Node xl xr ) = Node (maptree xl) (maptree xr)
it does work out.
I can not see the difference between the first function and the second one. How do I get error? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您缺少递归映射树调用上的函数:
应该是
You are missing the function on the recursive maptree calls:
should be
请注意,这是
Tree
类型的Functor
实例的明显fmap
。因此,您可以使用DeriveFunctor
扩展让 GHC 为您生成它。我们来试试吧。
Note that this is the obvious
fmap
of aFunctor
instance for yourTree
type. You can therefore use theDeriveFunctor
extension to have GHC generate it for you.Let's try it.
当您更深地递归时(对于这种高阶函数),不忘记传递函数的一种愚蠢方法是使用助手:
或者,或者(也许更常见):
在第一个示例中,我使用
go
有点像maptree f
的宏。在第二个示例中,我利用了maptree
的输入f
位于go
函数内部的事实,因为go
在maptree
的where
子句中声明。One dumb way to not forget to pass along the function as you recurse deeper (for this sort of higher-order function) is to use a helper:
Or, alternatively (and perhaps more commonly):
In the first example, I use
go
sort of as a macro formaptree f
. In the second example, I take advantage of the fact thatmaptree
's inputf
is in scope inside thego
function becausego
is declared in awhere
clause ofmaptree
.错误消息基本上告诉您出了什么问题:您没有传递
maptree
足够的参数。定义maptree f (Node xl xr)
表示maptree
接受两个参数:一个函数和一棵树。但是,当您像maptree xl
那样调用它时,您只给它一个参数(一棵树)。在第二个版本中,您将
maptree
定义为仅接受一个参数(一棵树),这就是它不会产生该错误的原因。例如,您可以通过调用
maptree f xl
而不是maptree xl
来解决问题。The error message basically tells you what's wrong: you are not passing
maptree
enough arguments. The definitionmaptree f (Node xl xr)
says thatmaptree
takes two arguments, a function and a tree. But when you call it likemaptree xl
, you're only giving it one argument (a tree).In your second version, you've defined
maptree
to only take one argument (a tree), which is why it doesn't produce that error.You can fix your problem by, for example, calling
maptree f xl
instead ofmaptree xl
.