1
data Geometry = Point {xCoord:: Double
                        ,yCoord:: Double} |Line  {xCoeff:: Double 
                      ,yCoeff:: Double
                       ,constant:: Double}|Plane {xCoeff:: Double,yCoeff:: Double, 
                       zCoeff::Double,
                       constant::Double} deriving (Show)

intersection:: Geometry -> Geometry -> Either String Geometry
intersection (Line a1 b1 c1) (Line a2 b2 c2)
                              | #### some code

intersection (Plane a1 b1 c1 d1) (Plane a2 b2 c2 d2)
                               | #### some code
                               | n1_n2_z /= 0 = Right $ParamerticLine (Point3D t1 (cond12/n1_n2_z) 0) (Point3D n1_n2_x n1_n2_y n1_n2_z)
                               | otherwise ## some code
                               where {Point t1 t2 = intersection (Line a1 b1 d1) (Line a2 b2 d2)}

I am trying to compute intersection of line in where clause of intersection of plane and using t1 in condition n1_n2_z/=0. I am getting error for where clause. Could I use intersection function as defined in where clause. What am I doing wrong in where clause?

device
  • 95
  • 6

1 Answers1

1

The reason this fails, is because intersect has as signature:

intersection:: Geometry -> Geometry -> Either String Geometry

but the left side of your where clause says:

where Point t1 t2 = intersection (Line a1 b1 d1) (Line a2 b2 d2)

so here your left operand has as type a Geometry, not an Either String Geometry.

You should thu capture this as:

where Right (Point t1 t2) = intersection (Line a1 b1 d1) (Line a2 b2 d2)

But this is unsafe, since it might happen that this is a `Left "some error message". You thus might want to use a pattern guard [Haskell-wiki] here:

# …
intersection (Plane a1 b1 c1 d1) (Plane a2 b2 c2 d2)
    | … = …
    | n1_n2_z /= 0, Right (Point t1 t2) <- intersection (Line a1 b1 d1) (Line a2 b2 d2) = Right $ ParamerticLine (Point3D t1 (cond12/n1_n2_z) 0) (Point3D n1_n2_x n1_n2_y n1_n2_z)
    | otherwise = …

Or you could take a look to the Monad (Either a) implementation, and thus use the bind to work with such Either a.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555