Haskell:可能的修正:add(Eq a)到的上下文 [英] Haskell: Possible fix: add (Eq a) to the context of

查看:309
本文介绍了Haskell:可能的修正:add(Eq a)到的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Haskell的新人,我很难理解这里的代码有什么问题。



这里是我



  data BinaryTree a = Empty |节点a(BinaryTree a)(BinaryTree a)

考虑形成镜像的函数reflect



  reflect :: BinaryTree a  - > BinaryTree a 
反映Empty = Empty
reflect(Node xlr)= Node x(reflect r)(reflect l)

写一个函数areMirrorImages确定两个二叉树t和u是否满足
t = reflect u。该函数不应该构建新的树,因此它不应该调用reflect或Node;


这是我写的:

  areMirrorImages :: BinaryTree a  - > BinaryTree a  - > bool 
areMirrorImages Empty Empty = True
areMirrorImages(Node _ _ _)Empty = False
areMirrorImages Empty(Node _ _ _)= False
areMirrorImages(Node xlr) ll rr)
| x == y =((镜像lrr)&&(are镜像rll))
|否则= False

当我尝试运行它时,我在第49行得到以下错误:

无法从使用'=='时产生的上下文()中推导出(等式a)

可能的修正:将add(Eq a)添加到'areMirrorImages'类型签名的上下文中

在表达式中:x == y



我很困惑为什么我得到这个错误,我尝试在线搜索解决方案,但我到目前为止没有发现。感谢。

解决方案

目前,您的二叉树可以容纳任何类型,即使是不能使用 == 。因此,如果你在包含这种类型的树上调用 areMirrorImages ...会发生什么?



要做的是在 areMirrorImages 函数上放置约束,以便它只能接受二进制树,它可以比较的内容。



如下所示:

  areMirrorImages :: Eq a => BinaryTree a  - > BinaryTree a  - > Bool 


I'm kind of new to Haskell and I'm having a hard time understanding what is wrong with my code here.

Here is what I'm supposed to do:
Consider the following definition of a binary tree

data BinaryTree a = Empty | Node a (BinaryTree a) (BinaryTree a)  

Consider the function reflect that forms the mirror image of a binary tree by exchanging left and right all the way down

reflect :: BinaryTree a -> BinaryTree a  
reflect Empty = Empty  
reflect (Node x l r) = Node x (reflect r) (reflect l)  

Write a function areMirrorImages that determines whether two binary trees t and u satisfy t = reflect u. The function should not build new trees, so it should not call reflect or Node; although it may use Node in patterns.

Here is what I've written:

areMirrorImages :: BinaryTree a -> BinaryTree a -> Bool  
areMirrorImages Empty Empty = True  
areMirrorImages (Node _ _ _) Empty = False  
areMirrorImages Empty (Node _ _ _) = False  
areMirrorImages (Node x l r) (Node y ll rr)  
    | x==y = ((areMirrorImages l rr) && (areMirrorImages r ll))  
    | otherwise = False  

When I try running it I get the following error on line 49:
Could not deduce (Eq a) from the context () arising from a use of '=='
Possible fix: add (Eq a) to the context of the type signature for 'areMirrorImages'
In the expression: x==y

I am confused as to why I'm getting this error and I tried finding solutions online but I have found nothing so far. Thanks.

解决方案

At the moment, your binary trees can hold any type whatsoever - even types that can't be compared using ==. So if you called areMirrorImages on a tree containing such a type ... what should happen?

What you need to do is place a constraint on the areMirrorImages function, so that it can only accept binary trees that it can compare the contents of.

Something like the following:

areMirrorImages :: Eq a => BinaryTree a -> BinaryTree a -> Bool

这篇关于Haskell:可能的修正:add(Eq a)到的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆