我刚开始学习Haskell.我正在尝试实现一个函数,它接受一个数字作为它的输入,并根据它的值返回-1,0或1.输入可以是任何数字(整数或浮点).这是代码:
signum :: (Num a) => a -> Int
signum x
| x > 0 = 1
| x < 0 = -1
| otherwise = 0
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将其加载到ghci时,它会显示以下错误:
[1 of 1] Compiling Main ( run_it.hs, interpreted )
run_it.hs:7:13:
Could not deduce (Ord a) arising from a use of `>'
from the context (Num a)
bound by the type signature for Main.signum :: Num a => a -> Int
at run_it.hs:5:11-29
Possible fix:
add (Ord a) to the context …Run Code Online (Sandbox Code Playgroud)