- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Elm 的新手,但不是函数式编程的新手,所以这个错误既令人沮丧又令人尴尬。我写了一个 50 行的 elm 程序,但我遇到了这些难以捉摸的类型错误。简而言之,有人能找到这段代码中的类型错误吗!!!
您可以将此代码直接粘贴到在线 elm editor .
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
最佳答案
我将您的代码放入在线编辑器中,它给了我很多预期/实际是 Int/Float 错误。我认为这是可以改进的地方,但那是针对邮件列表的。
您应该知道,编译器告诉您的预期/实际类型有时可以颠倒,至少对于某些人的直觉而言是这样。
为了调试这个问题,我首先阅读并尝试理解您的代码。代码很简单,但程序的目标对我来说并不是很清楚。无论如何,我没有发现任何不寻常的地方。我特别关注编译器指出类型错误所在的 main 代码行,但这似乎不是问题的根源。
所以我继续为还没有的函数添加类型注释。通常,当您添加更多类型注释时,编译器可以为您提供更好的定位。
我补充说:
render: (Int,Int) -> Tracker -> Element
main : Signal Element
toFloatT: (Int,Int) -> (Float,Float)
roundF: Float -> Float
然后编译器告诉我错误出在渲染函数中。在那里我注意到你制作了窗口尺寸的浮点值并且没有使用它们,然后使用了整数 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]
我希望通过向您展示我调试此类型错误的方法,您下次可以更轻松地找到解决方案。
问题是 render
功能:你给move
函数 Int
的元组s 代替 Float
的元组它期望。修复和编译代码可以找到here .
关于functional-programming - Elm 中的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19802016/
基本上我想在悬停时使文本带有删除线。这并不容易使用 el [ mouseOver [Font.strike] ] (text "some text") 就像这样 el [ mouseOver [Bac
基本上我想在悬停时使文本带有删除线。这并不容易使用 el [ mouseOver [Font.strike] ] (text "some text") 就像这样 el [ mouseOver [Bac
以下模式匹配至少有两个元素的列表有什么问题pos1和 pos2 ? type Pos = (Float, Float) type Tail = [Pos] tail_cut : Float -> Ta
我想更新 Elm 类型的记录 type CubeData = {currDirection : Vec3, translation : Vec3, transform : Mat4} 我可以使用以下代
假设有两种数据类型: type alias Player = { name : String , team : Team } type alias Team = { name : St
Python 有它们,我发现它们非常有用: def awesome_fn(x, y): """" Calculates some awesome function of x and y
我正在尝试在 Elm 中设计一个功能,该功能解析来自 Json 的数据,然后将其呈现在一个可排序的表中。 当然,我使用解码器将 Json 数据存储在记录列表中;然后在 View 中,我将记录列表转换为
假设我正在尝试遵循 Elm 架构并将我的工作流程拆分为 User s 和 Invoice s 使用 StartApp . 用户有发票,但他们必须登录才能访问它们。 该模型可能看起来像这样: type
我喜欢 React/Redux 的智能和哑组件概念,其中哑组件不处理自己的状态(Dump 组件对外界一无所知,它所做的只是根据其输入触发事件并显示值) .这是微不足道的,因为所有状态都在一个地方处理(
如何在 elm-repl 中找出 Elm 表达式或子表达式的类型? Haskell 的 :type或 :t相当于 Elm REPL 吗? 最佳答案 Elm REPL 会自动打印您输入的任何类型。例如:
在 Elm 中是否可以通过一个事件处理程序触发多个输入句柄?例如,假设我有以下内容: button [ type' "button", on "click" getAnything go.ha
我正在尝试构建一个通用映射函数,该函数将从联合类型中提取可能的内部类型,对内部值应用转换并将其映射回外部类型。 问题是,我需要一种方法来区分外部类型的特定值是否具有内部类型。 如果下面的代码实际编译了
每隔一段时间,您就会遇到如下编译器错误: It looks like the keyword `port` is being used as a variable. 这很烦人。这些关键词有完整的官方列
我刚刚开始考虑使用 Elm 构建一个简单的 Web 应用程序。我的想法是需要在浏览器中保留一些用户数据。 有没有办法直接用 Elm 处理数据持久化?例如在浏览器 session 甚至本地存储中?或者我
我有这个功能 result = add 1 2 |> \a -> a % 2 == 0) 我收到了这个错误 Elm does not use (%) as the remainder operat
编写一个函数将一个列表分成两个列表。第一部分的长度由调用者指定。 我是 Elm 的新手,所以我不确定我的推理是否正确。我认为我需要转换数组中的输入列表,以便我能够按提供的输入数字对其进行切片。我也在语
我正在尝试创建一个简单的Elm项目,仅插入“hello world!”串成div。 这是我的代码: index.html: ELM Course
我正在编写elm程序,该程序应将其输出格式化为HTML列表。我要的功能 inputs = ["first", "second", "third"] 并输出本质上是某种Elm Element,
在 Elm 中,我无法弄清楚什么时候 type 是合适的关键字,什么时候 type 别名 才是合适的。文档似乎没有对此进行解释,我也无法在发行说明中找到这一解释。这是否记录在某处? 最佳答案 我的想法
我刚刚开始学习 Elm,并且在理解为什么将自定义类型传递到期望的方法时遇到类型不匹配的问题......好吧,我称之为部分类型注释. 这是我正在使用的代码: import Graphics.Elemen
我是一名优秀的程序员,十分优秀!