我在Haskell中编写了一个函数,它在平面上占据了三个点,并检查它们是否在一条直线上,或是向右转还是向左转.
这是代码:
detDirection :: Point -> Point -> Point -> Direction
detDirection a@(Point (x1, y1)) b@(Point (x2, y2)) c
= if (collinear1 a b c)
then Straight
else let
ab = Vector [x2 - x1, y2 - y1]
angleAbX = angle ab (Vector [1, 0])
(Point (x1, y1)) = turnAtP a b angleAbX
(Point (x2, y2)) = turnAtP a c angleAbX
in if (y1 > y2)
then Right
else Left
Run Code Online (Sandbox Code Playgroud)
从来就测试collinear1,angle,turnAtP在ghci中,他们都立即终止.
detDirection然而,永远运行.
谁能告诉我这里的问题在哪里?
我想使函数Double - > Double一个Num类型类的实例.我想将两个函数的总和定义为它们的图像的总和.所以我写了
instance Num Function where
f + g = (\ x -> (f x) + (g x))
Run Code Online (Sandbox Code Playgroud)
在这里,编译器抱怨他无法判断我是否在lambda表达式中使用了Prelude.+或Module.+.所以我导入了Prelude qual作为P并写了
instance Num Function where
f + g = (\ x -> (f x) P.+ (g x))
Run Code Online (Sandbox Code Playgroud)
这编译得很好,但是当我尝试在GHCi中添加两个函数时,解释器再次抱怨他无法判断我是否正在使用Prelude.+或
Module.+.有什么办法可以解决这个问题吗?