修正Haskell中quicksort的一段代码“ Filter”的一部分。

Fen*_* Hu -2 haskell filtering list-comprehension

Filter :: (Ord a) => [a] -> [a]
 Filter [X:XS] = [[c|c<X, c<-XS ] ++ [X] ++ [c| c >X , c<-XS ]]
 quickSort :: Ord a => [a] -> [a]
 quickSort [] = []
 quickSort [x:xs] = quickSort mini ++ [x] + quickSort maxi
       where
           mini = filter xs
           maxi = filter xs   
Run Code Online (Sandbox Code Playgroud)

具有列表理解功能的“过滤器”功能正确吗?我知道Haskell-Libraries中有一个内置函数“ Filter”。但是我试图自己编写代码...

4ca*_*tle 5

:构造函数上进行模式匹配时,请使用(x:xs)[x:xs]是不同的。

始终将标识符的首字母小写。这是Haskell中的语法规则,而不仅仅是约定。

filter将类型的函数(a -> Bool)作为参数。它不需要Ord a实例。

在列表理解中,生成器项x <- xs必须位于使用x(即p x)的任何项的左侧。

filter :: (a -> Bool) -> [a] -> [a]
filter p xs = [x | x <- xs, p x]

quickSort :: Ord a => [a] -> [a]
quickSort [] = []
quickSort (x:xs) = quickSort mini ++ [x] ++ quickSort maxi
    where
        mini = filter (< x) xs
        maxi = filter (>= x) xs
Run Code Online (Sandbox Code Playgroud)