Chr*_*isU 2 functional-programming elm
我是elm的新手,但对于函数式编程并不陌生,所以这个错误既令人沮丧又令人尴尬.我写了一个50行榆树程序,但我得到了这些难以捉摸的类型错误.简而言之,有人可以在此代码中找到类型错误!
您可以将此代码粘贴到在线榆木编辑器中.
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
Run Code Online (Sandbox Code Playgroud)
我把你的代码放在在线编辑器中,它给了我很多预期/实际的Int/Float错误.我认为这是可以改进的东西,但这是针对邮件列表的.
你应该知道的是,编译器告诉你的预期/实际类型有时可以逆转,至少对某些人的直觉是这样.
要调试此问题,我首先阅读并尝试了解您的代码.代码很简单,但程序的目标并不是我立即清楚的.无论如何,我没有发现任何与众不同的东西.我主要关注编译器说类型错误的主代码行,但这似乎不是问题的根源.
所以我继续向没有任何功能的函数添加了类型注释.通常,当您添加更多类型注释时,编译器可以为您提供更好的精确定位.
我补充说:
render: (Int,Int) -> Tracker -> Element
main : Signal Element
toFloatT: (Int,Int) -> (Float,Float)
roundF: Float -> Float
Run Code Online (Sandbox Code Playgroud)
然后编译器能够告诉我错误是在render函数中.在那里,我注意到,你做的窗口尺寸的浮点值,并没有使用它们,以及之后所使用的整数x和y的Tracker在一个元组move.并且存在错误,因为move需要一个浮点元组.
因此,当您使用以下适应的渲染函数时,事情编译:
render: (Int,Int) -> Tracker -> Element
render (w',h') trk =
let trkPos = toFloatT (trk.x, trk.y)
in collage w' h'
[ngon 3 20 |> filled green
|> move trkPos
, asText (trk.th) |> toForm]
Run Code Online (Sandbox Code Playgroud)
我希望通过向您展示调试此类型错误的方法,您可以在下次更轻松地找到解决方案.
问题是render函数:你给move函数一个Ints 元组而不是Float它期望的元组.可以在此处找到修复和编译代码.