我在此代码中遇到问题:
module Blueprint where
data Colour = Blue | Green | Red
deriving ( Eq, Show )
data Car = Car { wheels :: Integer
, colour :: Colour
}
deriving ( Eq, Show )
data Property = Colour_Is Colour
| Wheels Ordering Integer
| And Property Property
| Not Property
| Or Property Property
deriving Show
check :: Property -> Car -> Bool
check prop car = case prop of
Colour_Is col -> col == colour car
Wheels ord num -> case ord of
LT -> num > wheels car
EQ -> num == wheels car
And l r -> check l car && check r car
Not p -> not check p car
Or x y -> check x car || check y car
cars = [ Car { wheels = 4, colour = Red }
, Car { wheels = 2, colour = Blue }
, Car { wheels = 14, colour = Green }
, Car { wheels = 4, colour = Green }
, Car { wheels = 2, colour = Red }
]
prop1 :: Property
prop1 = And (Wheels EQ 14) (Colour_Is Green)
test :: Bool
test = and
[ check ( Wheels EQ 4 ) ( cars !! 0 )
, check ( Wheels LT 3 ) ( cars !! 1 )
, check ( And ( Wheels EQ 14 ) ( Colour_Is Green )) ( cars !! 2 )
, check ( Not ( Colour_Is Red ) ) ( cars !! 3 )
, filter ( check prop1 ) cars == take 3 cars
]
我构建了check函数并正确实现了Colour_Is,And,Not和Wheels,但是当我添加Or函数时:
( Or x y -> check x car || check y car )
我从ghci收到此错误:
d:\ My_data \ HS \ strategy.hs:26:16: 输入` - >'解析错误失败,模块加载:无。
我是Haskell的新手。我的错误在哪里?
答案 0 :(得分:3)
您的解析错误可能是由于缩进不正确,但Dhaivat重新格式化您的问题后该错误不存在。
此外,您需要围绕not
的参数:
Not p -> not check p car
-->
Not p -> not (check p car)
除了正确格式化您的问题,下次请选择一个合理的标题。 “Haskell不起作用”的标题是a)错误的b)没有信息c)不能从喜欢Haskell的人那里得到答案。它仍然是一个足够小的社区,可能对语言进行个人投资的人(那些从事语言规范和核心图书馆工作的人)实际上是该语言社交场景的一部分。