Haskell function does not end

I wrote a function in Haskell that takes three points in a plane, and checks to see if they are in a straight line or make a right or left turn.

Here is the code:

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

I tested collinear1, angle, turnAtPin GHCi, and they all come to an end immediately. detDirectionhowever, continues to work forever.

Can someone tell me where the problem is?

+5
source share
1 answer

In Haskell, it letis a recursive binding, that is, you can refer to variables declared in an expression letin the defining expressions of other variables. So when you write

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

x1, x2, y1 y2 , , let. Point, ,

        (Point (x3, y3))    = turnAtP a b angleAbX
        (Point (x4, y4))    = turnAtP a c angleAbX

, .

+15

All Articles