【问题标题】:Defining Own Data Type in Haskell Where clause在 Haskell Where 子句中定义自己的数据类型
【发布时间】:2020-05-19 03:39:35
【问题描述】:
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 子句中做错了什么?

【问题讨论】:

  • 你能分享一下错误吗?

标签: haskell template-haskell


【解决方案1】:

失败的原因是intersect 有签名:

intersection:: Geometry -&gt; Geometry -&gt; <b>Either String Geometry</b>

但是where 子句的左侧说:

where <b>Point</b> t1 t2 = intersection (Line a1 b1 d1) (Line a2 b2 d2)

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

您应该将其捕获为:

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

但这是不安全,因为它可能会发生“留下”一些错误消息“。因此,您可能想在这里使用pattern guard [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

【讨论】:

    猜你喜欢
    • 2013-10-15
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    相关资源
    最近更新 更多