首页 > 解决方案 > 在 Haskell Where 子句中定义自己的数据类型

问题描述

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)}

我正在尝试计算平面交集的 where 子句中的线交点,并在条件 n1_n2_z/=0 中使用 t1。我收到 where 子句的错误。我可以使用 where 子句中定义的交集函数吗?我在 where 子句中做错了什么?

标签: haskelltemplate-haskell

解决方案


失败的原因是因为intersect有签名:

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

但是您的条款的左侧where说:

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

所以这里你的左操作数的类型是 a Geometry,而不是 a Either String Geometry

您应该将其捕获为:

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

但这是不安全的,因为它可能会发生“留下“一些错误消息”。因此,您可能希望在此处使用模式保护[Haskell-wiki]

# …
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 = …

或者您可以查看Monad (Either a)实现,从而使用绑定来处理此类Either a.


推荐阅读