[英]Could not match expected type with actual type in Haskell
I've written the following code:我编写了以下代码:
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)
I could not understand the following ERROR我无法理解以下错误
What's the problem?有什么问题?
Thanks in advance.提前致谢。
You aren't applying updateSystem time
to a value of type System
;您没有将
updateSystem time
应用于System
类型的值; you are applying it to the data constructor System
and a list of Body
values xs
.您将它应用于数据构造函数
System
和Body
值列表xs
。 You need to adjust your parentheses to create the necessary value of type System
.您需要调整括号以创建
System
类型的必要值。
updateSystem time (System(x:xs)) = moveBody time x : updateSystem time (System xs)
However, you can simply the definition of updateSystem
by using map
on the list that System
wraps.但是,您可以通过在
System
包装的列表上使用map
来简单地定义updateSystem
。
updateSystem :: Double -> System -> System
updateSystem time (System xs) = System (map (moveBody time) xs)
Extract the value of type [Body]
, map moveBody time
over it to get a new list of Body
's, and rewrap the list as a new System
value.提取类型
[Body]
的值,将moveBody time
映射到它上面以获得Body
的新列表,然后将该列表重新包装为新的System
值。
You can define custom prepending operator for your type, eg .:
to wrap list prepending for System
:您可以为您的类型定义自定义前置运算符,例如
.:
为System
包装列表前置:
type Mass = Double
type Position = (Double, Double)
type Velocity = (Double, Double)
data Body = Body Mass Position Velocity
data System = System [Body]
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))
(.:) :: Body -> System -> System
(.:) b (System x) = System (b : x)
updateSystem :: Double -> System -> System
updateSystem time (System []) = (System [])
updateSystem time (System(x:xs)) = moveBody time x .: updateSystem time (System xs)
You get an error because System
is not a list, so you can not prepend elemens to it just by using :
operator.您会收到错误消息,因为
System
不是列表,因此您不能仅使用:
运算符在其前面添加元素。
You also had a bad notation of System(xs)
instead of (System xs)
, you should to construct System
from list at first, and pass it to functions after this.您也有一个错误的
System(xs)
符号而不是(System xs)
,您应该首先从列表构造System
,然后将其传递给函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.