haskell - 无法将预期类型与 Haskell 中的实际类型匹配
问题描述
我编写了以下代码:
type Mass = Double
type Position = (Double, Double)
type Velocity = (Double, Double)
data Body = Body Mass Position Velocity
data System = System [Body]
renderBody :: Body -> Picture
renderBody (Body mass (x, y) velocity) = object
where
object = translated x y (solidCircle mass)
renderSystem :: System -> Picture
renderSystem (System []) = blank
renderSystem (System (x:xs)) = renderBody(x)<>renderSystem(System(xs))
moveBody :: Double -> Body -> Body
moveBody time (Body mass (x, y) (vx, vy)) = new_body
where
new_x = x + vx * time
new_y = y + vy * time
new_body = (Body mass (new_x, new_y) (vx, vy))
updateSystem :: Double -> System -> System
updateSystem time (System []) = (System [])
updateSystem time (System(x:xs)) = moveBody time x : updateSystem time System(xs)
我无法理解以下错误
有什么问题?
提前致谢。
解决方案
您没有申请updateSystem time
type 的值System
;您将其应用于数据构造函数System
和Body
值列表xs
。您需要调整括号以创建必要的 type 值System
。
updateSystem time (System(x:xs)) = moveBody time x : updateSystem time (System xs)
updateSystem
但是,您可以通过使用map
对System
换行的列表来简化定义。
updateSystem :: Double -> System -> System
updateSystem time (System xs) = System (map (moveBody time) xs)
提取 type 的值[Body]
,对其进行映射moveBody time
以获取Body
's 的新列表,并将列表重新包装为新System
值。
推荐阅读
- java - 为什么我有这个错误?ArrayIndexOutOfBoundsException
- php - 在特定句子之间插入标签,保持原始大小写
- javascript - 在一个 div 中编写 HTML 标签,在另一个 div 中查看所见即所得
- c++11 - 管理单个内存块的分配器
- c# - Aspose 在 MS Powerpoint 中查找给定单词
- java - 如何在android中每24小时运行一次代码
- c# - 在 iis 上从字节数组加载图像失败
- ios - 邀请某人作为 Apple Developer Program 的管理员,但他无法访问 App Store Connect
- symfony - 实体中的 Symfony 4 自动装配不起作用
- php - contact_us.php 没有向我的网站相关邮件地址发送电子邮件