【发布时间】:2011-01-08 21:53:23
【问题描述】:
我在 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
我在 GHCi 中测试了collinear1、angle、turnAtP,它们都立即终止。
但是,detDirection 会一直运行下去。
谁能告诉我问题出在哪里?
【问题讨论】:
-
试过逐行遍历它吗?
-
打开警告(GHCi 中的
:set -Wall),您会清楚地看到您做错了什么。