Haskell,对Int列表进行排序

时间:2018-12-04 13:49:03

标签: haskell

我需要对一个Ints列表进行排序,[Int]。但是,当我使用sort时,会给我错误消息:变量不在范围内:s​​ort :: [Int]-> [a0]如何解决此问题?

我的代码简化了:

     data Point= P Shape Int
     getInt (P _ i)=i
     sorted::[Point]->[Int]
     sorted ps= sort(map getInt ps)

1 个答案:

答案 0 :(得分:1)

如果要搜索知道(或猜测)名称但不知道其定义位置的函数,Hayoo是最好的朋友。要求提供sortit'll give

sort :: Ord a => [a] -> [a]
base -Data.List  
> The sort function implements a stable sorting algorithm. It is a special case of sortBy, which allows the programmer to supply their own comparison function.

sort :: Ord a => [a] -> [a]
base -GHC.OldList  
> The sort function implements a stable sorting algorithm. It is a special case of sortBy, which allows the programmer to supply their own comparison function.

sort :: ByteString -> ByteString
bytestring -Data.ByteString.Char8  Data.ByteString  
> O(n) Sort a ByteString efficiently, using counting sort.

...

好吧,您不需要字节字符串(或Seq或其他更高级的类型),也不需要触摸名为GHC.OldList的模块(这是一些遗留的东西,可用于快速使旧代码与新GHC版本兼容); Data.List版本显然很好。因此导入:

import Data.List (sort)

main :: IO ()
main = print $ sort [3,1,2]

Hayoo是两个流行的Haskell搜索引擎之一,可以更好地按名称搜索IMO。替代方案Hoogle有点儿麻烦,但还可以通过类型签名进行搜索。我推荐the Stackage version