如何在haskell中修复"函数中的非详尽模式"错误?

s_d*_*onu 0 haskell tuples

我正在尝试创建一个函数,它将元组列表作为参数并按第二个元素排序.它不打印任何其他,只是错误'***异常:main.hs:20:1-76:函数sortWords中的非详尽模式'这是代码:

sortWords :: [(String, Int)] -> [(String, Int)]

sortWords [(str,num)] = sortBy (\x y -> compare (snd x) (snd y)) [(str,num)]`
Run Code Online (Sandbox Code Playgroud)

以下是我如何调用该函数

main = do
    putStrLn $ show $ sortWords [("friend",1),("she",2)]
Run Code Online (Sandbox Code Playgroud)

我不得不说我在http://Repl.it网站上运行我的程序

谢谢!

Tho*_*son 6

sortWords [(str,num)] = 
Run Code Online (Sandbox Code Playgroud)

您的函数定义(上图)模式匹配包含单个元素的列表,该元素是具有两个值中每个值的变量的元组.

您似乎只想要一个变量而不是模式匹配:

sortWords xs  = sortBy (\x y -> compare (snd x) (snd y)) xs
Run Code Online (Sandbox Code Playgroud)

或者减少:

sortWords = sortBy (\x y -> compare (snd x) (snd y))
Run Code Online (Sandbox Code Playgroud)