小编rya*_*yan的帖子

Haskell 如何推断类型类的定义?

所以我在玩 Haskell 并注意到一些让我困惑的事情。我定义了一个复杂的浮点数据结构,并想在它上面使用比较运算符。最初我这样做了,效果很好:

data Cplx = Cplx Float Float deriving (Eq, Show)

instance Ord Cplx where
  (<=) a b = (<=) (normCplx a) (normCplx b)
  (>=) a b = (>=) (normCplx a) (normCplx b)
  (<) a b = (<) (normCplx a) (normCplx b)
  (>) a b = (>) (normCplx a) (normCplx b)

normCplx :: Cplx -> Float
normCplx (Cplx a1 a2) = sqrt( a1^2 + a2^2)
Run Code Online (Sandbox Code Playgroud)

但我也注意到刚刚声明:

data Cplx = Cplx Float Float deriving (Eq, Show)

instance Ord Cplx where …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
206
查看次数

无法将预期类型`IO()'与实际类型`a0 - > m0 a0'匹配

这是我的代码:

doSomething :: IO Bool -> IO () -> IO ()
doSomething cond body = cond >>= ( \condition -> if condition then return else body )
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误:

Couldn't match expected type `IO ()' with actual type `a0 -> m0 a0'
In the expression: return
In the expression: if condition then return else body
In the second argument of `(>>=)', namely
  `(\ condition -> if condition then return else body)'
Run Code Online (Sandbox Code Playgroud)

我也试过这个等价的符号:

whileM :: IO Bool -> IO () -> IO …
Run Code Online (Sandbox Code Playgroud)

haskell

1
推荐指数
1
解决办法
881
查看次数

标签 统计

haskell ×2