haskell - 为什么会出现“No instance for (Arbitrary) from a use of `quickCheck'”错误?
问题描述
我是 Haskell 的新手,我在处理这个错误时遇到了一些麻烦。我在 Windows 上使用 ghci。这是代码:
data Direction = North | South | East | West
deriving (Eq, Show)
type Point = (Int, Int)
origin = (0,0)
type Road = [Direction]
movement :: Point -> Road -> Point
{- ... }
test :: Road -> Road -> Bool
test road1 road2 = movement origin road1 == movement origin road2
-- i check if two roads lead to the same destination starting from
-- the origin point in a grid
这是我尝试运行测试时发生的情况:
*Main> quickCheck test
<interactive>:8:1: error:
* No instance for (Arbitrary Direction)
arising from a use of `quickCheck'
* In the expression: quickCheck test
In an equation for `it': it = quickCheck test
我的老师告诉我我的代码是正确的,但没有解释为什么会在 Windows 上发生这种情况,因此没有提供解决方案。我也没有在网上找到任何有用的东西。我真的很感激解释。
解决方案
你定义了:
Direction = North | South | East | West
deriving (Eq, Show)
上面没有instance Arbitrary Direction
。毕竟,怎么可能呢?您刚刚定义了方向,整个世界中唯一的实例是Eq
和Show
。
尝试:
import Test.QuickCheck
data Direction = North | South | East | West
deriving (Eq,Show)
instance Arbitrary Direction where
arbitrary = elements [North,South,East,West]
该elements
功能来自Test.QuickCheck
就像Arbitrary
.
作为元注释:如果您的讲师没有立即看到这里的问题,那么可能是沟通不畅,或者您应该计划补充您的 Haskell 教育,例如使用 wikibooks 等在线资源、函数式编程工艺等印刷材料,或在freenode这样的地方有很多对话。
推荐阅读
- c - 为什么我的代码即使正确也不起作用?
- java - 普通类偶尔返回空值
- android - 不返回来电号码
- acumatica - 如何在客户端获取字段或变量的信息
- python - 名称 'latest_change_date' 未定义,如何按日期时间排序
- python - 运行数据框与部分并行应用
- eventhandler - 父页面订阅嵌套子控件事件处理程序的问题
- django - 关于 Django 的问题。我的客户只想通过单击或其他方式添加其他选择。我可以解决这个问题吗?
- docker - Kubernetes 重复 pod kube-system/metrics-server 收到错误
- algorithm - 如何衡量时间序列的“同质性”?