我是Haskell的新手,并使用GHCi,版本7.6.3
我试图理解为什么这个函数赋值(或替换,无论正确的术语是什么)都不起作用。
此代码可以正常工作:
import qualified Data.List as L
testSort list = L.sort list
提示:
*Main> testSort [3,2,1]
[1,2,3]
但是,如果我删除list参数...
import qualified Data.List as L
testSort = L.sort
对我而言,直观地说,这可以简单地将L.sort替换为testSort,然后我可以像以前一样在提示符下运行相同的命令并得到相同的结果。但是,我从GHCi那里得到了一个很大的错误:
No instance for (Ord a0) arising from a use of `L.sort'
The type variable `a0' is ambiguous
Possible cause: the monomorphism restriction applied to the following:
testSort :: [a0] -> [a0] (bound at modulesandbox.hs:4:1)
Probable fix: give these definition(s) an explicit type signature
or use -XNoMonomorphismRestriction
Note: there are several potential instances:
instance Integral a => Ord (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
instance Ord () -- Defined in `GHC.Classes'
instance (Ord a, Ord b) => Ord (a, b) -- Defined in `GHC.Classes'
...plus 23 others
In the expression: L.sort
In an equation for `testSort': testSort = L.sort
有没有办法设置我的代码,以便testSort = L.sort
可以表现为简单的函数替换?
答案 0 :(得分:9)