我编写了一个代码来对元组列表的索引进行排序我尝试将map与bubblesort()
一起使用
避免使用循环
bubblesort::(Ord t) => [t]->[t]
bubblesort[x,y,z,xs]=
if x<y then x : map bubblesort [y,z,xs]
else y : map bubblesort [x,z,xs]
但它给了我一个错误:
ERROR line 20 - Type error in list *** Expression : [y,xs] *** Term : xs *** Type : [a] *** Does not match : a *** Because : unification would give infinite type
*注意请仅给我指示
--编译器为在线编译器
最佳答案
我不知道这个问题的完整解决方案,但 bubblesort 需要 [t]
,但 bubblesort [x,z,xs]
将是 的函数>[[t]] -> [[t]]
。因此每次,类型都会包裹在另一个 []
此外,您不需要进行映射冒泡排序
。尝试记住 map
函数的确切作用,看看为什么这没有意义。
关于按索引对 haskell 中的元组数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11509552/