我应该填写undefined
的内容来编译程序并对其进行测试。我真的不知道symdiff应该做什么,所以我不知道我可以为undefined
填写什么。有人可以给我一个提示,我可以插入undefined
吗?
顺便说一句,当我想用ghci 7.6.3编译代码时,我收到一个错误:
Could not find module 'Test.SmallCheck.Series'
我该如何解决这个问题?
以下是代码:
{-# language FlexibleInstances #-}
{-# language MultiParamTypeClasses #-}
{-# language NoMonomorphismRestriction #-}
module Blueprint where
import Test.SmallCheck
import Test.SmallCheck.Series
data N = Z | S N deriving (Show , Eq)
symdiff :: N -> N -> N
symdiff x y = undefined
-- for testing in ghci: smallCheck 10 spec1
spec1 = \ (x,y) -> symdiff x y == symdiff y x
spec2 = \ (x,y) -> symdiff x (plus x y) == y
plus :: N -> N -> N
plus x y = case x of
Z -> y
S x' -> S (plus x' y)
test :: Bool
test = and
[ null $ failures 10 1000 $ spec1
, null $ failures 10 1000 $ spec2
]
instance Monad m => Serial m N where series = cons0 Z \/ cons1 S
-- | first f failures from t testcases for property p
failures f t p = take f
$ filter ( \ x -> not $ p x )
$ take t
$ do d <- [ 0 .. ] ; list d series
谢谢,这帮了很多忙!怎么样:
symdiff :: N -> N -> N
symdiff x y = case x of
Z -> y
S x' -> case y of
Z -> x
S y' -> ???
这些行是否正确(除了带有???的行,我已经考虑过它了)
这适用于最后一行:
S y' -> symdiff x' y'
答案 0 :(得分:6)
这些提示属于小测试:
spec1 = \ (x,y) -> symdiff x y == symdiff y x
spec2 = \ (x,y) -> symdiff x (plus x y) == y
这意味着symdiff
需要满足两个方程式
symdiff x y == symdiff y x -- shouldn't matter what order (symmetric?)
symdiff x (plus x y) == y -- if they differ by y, that's the answer (difference)
这意味着symdiff
必须找到两个参数之间的差异。
您需要将Z
视为零,将S x
视为x
的后继者,即x
+ 1。
好消息是N
只有两种可能性,因此symdiff
最多有四种方程式:
symdiff Z Z =
symdiff Z (S y) =
symdiff (S x) Z =
symdiff (S x) (S y) =
想想你如何找到之间的区别
0和0
0和1 + y
1 + x和0
1 + x和1 + y
帮助你。
(您可以简化为更少的案例,但从这四个案例开始。)
现在就考虑一下,如果在一个好的思考之后这还不够暗示,请评论。