首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用导入的函数替换

使用导入的函数替换
EN

Stack Overflow用户
提问于 2015-01-06 08:19:38
回答 1查看 94关注 0票数 1

我是一个Haskell新手,和GHCi一起工作,版本为7.6.3

我试图理解为什么这个函数分配(或替换,无论是什么正确的术语)不起作用。

此代码运行良好:

代码语言:javascript
复制
import qualified Data.List as L
testSort list = L.sort list

迅速:

代码语言:javascript
复制
*Main> testSort [3,2,1]
[1,2,3]

但是,如果我删除了像这样的列表参数.

代码语言:javascript
复制
import qualified Data.List as L
testSort = L.sort

对我来说,这很直观,这将简单地替代L.sort来代替testSort,然后我就可以像以前一样在提示符下运行相同的命令并得到相同的结果。但是,我从GHCi中得到了一个很大的错误:

代码语言:javascript
复制
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可以作为一个简单的函数替换?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-06 08:22:34

可怕的单态约束再次袭击。

只要养成习惯,总是写出类型签名,那么这将不会是一个问题。

代码语言:javascript
复制
testSort :: Ord a => [a] -> [a]
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27794660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档