我正在尝试在 haskell 中创建一个函数,以了解列表列表中的所有元素是否具有相同的长度。(我在以前的帖子中搜索过答案,但没有一个有效)。
sameLength :: [[t]] -> String
sameLength [] = "Empty list"
sameLength [[items]]
| and $ map (\x -> length x == (length $ head [[items]])) [[items]] = "Same length"
| otherwise = "Not the same length"
问题是它不起作用:
*Main> :l test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> sameLength []
"Empty list"
*Main> sameLength [[1,2],[3,4]]
"*** Exception: test.hs:(2,1)-(5,39): Non-exhaustive patterns in function sameLength
*Main> sameLength [[1,2]]
"*** Exception: test.hs:(2,1)-(5,39): Non-exhaustive patterns in function sameLength
我真的不明白问题出在哪里。它处理参数是空列表和不是空列表的情况。我错了吗 ?我错过了什么 ?
谢谢你的帮助 :)