0
我正在尝试学习Haskell,并且尝试完成示例问题时遇到了问题。问题是根据给定的谓词排序在Haskell列表即类型是根据谓词对haskell中的列表进行排序
sort :: (a -> a -> Bool) -> [a] -> [a]
我到目前为止的代码:
sort _ [] = []
sort f (x:xs) =
let
smaller = sort f (filter (f x) xs)
bigger = sort f (filter (f x) xs) --error is on this line
in smaller ++ [x] ++ bigger
不是在这个意义上IM正常工作,而不是代码确定如何采取相反的功能。例如,如果它是一个普通的排序函数,我会使用smaller = quicksort (filter (<=x) xs)
和bigger = quicksort (filter (>x) xs)
这将根据谓词分解列表,但是如何使用更高阶的谓词来做到这一点?
首先,你需要缩进'smaller'和'更大一些,以便他们通过'let'。其次,你可能想看看'not'函数。 – bheklilr
@bheklilr似乎这样编译得很好。 “not”函数应该像“(not f x)”一样使用吗?因为这给我编译器错误。 – Ben
尝试'(不是(f x))或'(不是$ f x)','f'不是'not'的参数,'f x'是你想给它的参数。 – bheklilr