我是 elm 新手,但对函数式编程并不陌生,所以这个错误既令人沮丧又令人尴尬。我写了一个 50 行的 elm 程序,但我得到了这些难以捉摸的类型错误。简而言之,有人能在这段代码中找到类型错误吗!!!
您可以将此代码直接粘贴到在线elm 编辑器中。
import Mouse
import Window
--Model
type Tracker = {x:Int, y:Int, th:Float}
tracker:Tracker
tracker = {x=100, y=100, th=0.0}
trkS:Signal Tracker
trkS = constant tracker
dir: Tracker -> (Int, Int) -> (Int,Int) -> Float
dir t (x',y') (w',h') =
let (x,y) = toFloatT (x',y')
(w,h) = toFloatT (w',h')
(dx, dy) = (x - w/2, h/2 - y)
in (atan2 (dy - (toFloat t.y)) (dx - (toFloat t.x)))
dirS:Signal Float
dirS = lift3 dir trkS Mouse.position Window.dimensions
changeV: Float -> Tracker -> Tracker
changeV theta t =
{t | th <- theta }
moveTracker: Int -> Tracker -> Tracker
moveTracker time' t =
let time = toFloat time'
x' = (toFloat t.x) + 3 * time *(cos t.th)
y' = (toFloat t.y) + 3 * time *(sin t.th)
in {t | x <- round x'
, y <- round y'}
step:(Int, Float) -> Tracker -> Tracker
step (dt, dir) = moveTracker dt . changeV dir
render (w',h') trk =
let (w,h) = (toFloat w', toFloat h')
in collage w' h'
[ngon 3 20 |> filled green
|> move (trk.x, trk.y)
, asText (trk.th) |> toForm]
input:Signal (Int,Float)
input =
let delta = lift (round . (\t -> t/20)) (fps 25)
in sampleOn delta (lift2 (,) delta dirS)
main =
lift2 render Window.dimensions (foldp step tracker input)
--Helper functions
toFloatT (x,y) = (toFloat x, toFloat y)
roundF = toFloat . round