奇怪的Haskell/GHCi问题

hor*_*s83 5 haskell functional-programming ghci

所以我有一些代码*,当拿三分时,应该返回一个方向.我写过这个解决方案,但每次尝试运行它都会导致GHCi冻结,所以我想知道我做错了什么.这是代码:

--chapter 3 question 9
data Point x y = Point x y deriving (Eq, Show)
data Vector x y = Vector x y deriving (Eq, Show)

sub (Point x y) (Point a b) = (Vector (x-a) (y-b))
dot (Vector x y) (Vector a b) = (x*a)+(y*b)
perp (Vector x y) = (Vector (-y) x)
mag (Vector x y) = sqrt (dot v v) where v = (Vector x y)

data Direction = LeftTurn | RightTurn | Straight | Reverse | Stop | Undefined 
    deriving (Eq, Show)
getDirection (Point a b) (Point c d) (Point e f) 
    | a/=c && b/=d && c==e && d==f = Stop
    | a==c && b==d || c==e && d==f || e==a && f==b = Undefined
    | d > 0 = LeftTurn
    | d < 0 = RightTurn
    | otherwise = Straight
    where d = dot (sub p1 p0) (perp (sub p2 p1))
          where p0 = (Point a b)
                p1 = (Point c d)
                p2 = (Point e f)
Run Code Online (Sandbox Code Playgroud)

我看不到递归,所以我不明白它为什么会这样.到目前为止,Haskell编译器一直非常直言不讳地告诉我什么时候我做了一些愚蠢的事情,但是编译得很好.

*如果您想知道,这是"真实世界Haskell"第3章的问题9.

sep*_*p2k 7

你绑定了两次名字.首先在模式中Point c d比在where子句中.

因此,如果您尝试d通过模式访问绑定,则实际上d是以where递归方式引用from 子句.

  • 如果您使用-Wall运行GHCi,您将收到有关此问题的警告. (5认同)
  • 或者甚至更好,将":set -Wall"行添加到〜/ .ghci文件中. (2认同)