簡體   English   中英

無法將預期類型與 Haskell 中的實際類型匹配

[英]Could not match expected type with actual type in 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應用於System類型的值; 您將它應用於數據構造函數SystemBody值列表xs 您需要調整括號以創建System類型的必要值。

updateSystem time (System(x:xs)) = moveBody time x : updateSystem time (System xs)

但是,您可以通過在System包裝的列表上使用map來簡單地定義updateSystem

updateSystem :: Double -> System -> System
updateSystem time (System xs) = System (map (moveBody time) xs)

提取類型[Body]的值,將moveBody time映射到它上面以獲得Body的新列表,然后將該列表重新包裝為新的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)

您會收到錯誤消息,因為System不是列表,因此您不能僅使用:運算符在其前面添加元素。

您也有一個錯誤的System(xs)符號而不是(System xs) ,您應該首先從列表構造System ,然后將其傳遞給函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM