haskell - 在 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 子句中做错了什么?
解决方案
失败的原因是因为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
.
推荐阅读
- vba - 收到电子邮件的自动类别分配 + 文件名附件
- sql - 未从视图中获取数据
- html - 是否有其他方法可以从 Amazon S3 访问对象?
- powerbi - 数据排序问题
- javascript - SPARQL 查询从 SPARQL 查询验证器获取错误,但在 Virtuoso SPARQL 查询编辑器中有效
- python - 地理编码返回错误的纬度/经度
- r - 使用 ggplot 可视化因子水平之间的差异
- javascript - 第三方库 (react-dev-utils/webpackHotDevClient) 在 require('chalk') 上获取空对象
- javascript - 错误:BrowserModule 已加载。在 MDB-Angular 延迟加载中
- c# - 是否有任何等效的 ServiceDefinition.csdef 用于发布到 App 服务?