Следующий код предназначен для создания либо Double, либо Integer. s
предполагается равным negate
или id
; n
вся часть; и f
дробная часть или Ничего
для целого числа.
computeValue :: Num a => (a->a) -> Integer -> (Maybe Double) -> Either Double Integer
computeValue s n Nothing = Right $ s n
computeValue s n (Just a) = Left $ s (fromIntegral n + a)
когда я компилирую это, я получаю:
test1.hs:2:28:
Couldn't match type `Integer' with `Double'
Expected type: Either Double Integer
Actual type: Either Double a
In the expression: Right $ s n
In an equation for `computeValue':
computeValue s n Nothing = Right $ s n
test1.hs:2:38:
Couldn't match type `Integer' with `Double'
In the first argument of `s', namely `n'
In the second argument of `($)', namely `s n'
In the expression: Right $ s n
Похоже, компилятор каким-то образом упустил из виду тот факт, что s
является полиморфным. Что здесь произошло и как это исправить?