【问题标题】:Couldn't match expected type - Haskell无法匹配预期类型 - Haskell
【发布时间】:2011-11-30 09:32:29
【问题描述】:

我是 Haskell 的新手,仍然无法完全弄清楚这些类型的问题。我收到以下功能的错误:

computeTriUp :: Point -> Float -> [Point]
computeTriUp center r = [(x + r*cos(pi/2.0), y+r*sin(pi/2.0)), (x+r*cos(5.0*pi/4.0),       y+r*sin(5.0*pi/4.0)), (x+r*cos(7.0*pi/4.0), y+r*sin(7.0*pi/4.0))]
    where x = fst center
          y = snd center

我的错误是:

Couldn't match expected type `Int' with actual type `Float'
In the first argument of `(*)', namely `r'
In the second argument of `(+)', namely `r * cos (pi / 2.0)'
In the expression: x + r * cos (pi / 2.0)

有什么想法吗? 谢谢!

【问题讨论】:

  • Point的定义是什么?
  • 另外,最好在(x,y) 上进行显式模式匹配,除非您打算稍后替换Point 的定义,并且这样做可以最大限度地减少代码更改...

标签: haskell types floating-point int


【解决方案1】:

假设 Point 是一对 Ints,您可能想要更改 x 和 y 的定义以将它们转换为 Floats...

x = fromIntegral $ fst centre

【讨论】:

  • 这会遇到问题,因为结果也被声明为Points 的列表。我认为更好的选择是让Point 包含Floats...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 2020-09-05
  • 2020-03-17
  • 1970-01-01
  • 2016-07-18
相关资源
最近更新 更多