- 论坛徽章:
- 0
|
实现对字符串的去头尾空白,按字符串分割,合并。
module String where
import Data.List
import Data.Maybe
stripPreBlank :: String -> String
stripSurBlank :: String -> String
stripBlank :: String -> String
join :: String -> [String] -> String
split :: String -> String -> [String]
stripPreBlank str =
head $ filter (\s ->
not $ or $ map (\t ->
isPrefixOf t s
) [" ", "\n", "\t", "\r"]
) $ tails str
stripSurBlank str =
reverse $ stripPreBlank $ reverse str
stripBlank str =
stripPreBlank $ stripSurBlank str
join tok strs =
foldl1 (\a b ->
a ++ tok ++ b
) strs
split tok str =
let tmp = filter (isPrefixOf tok) $ tails str in
map (drop $ length tok) $ zipWith (\a b ->
take ((length a) - (length b)) a
) ([tok ++ str] ++ tmp) $ tmp ++ [""] |
归并排序,改成稳定排序了
module List where
import Data.List
sortMerge :: (a -> a -> Bool) -> [a] -> [a]
sortMerge_ :: (a -> a -> Bool) -> ([a], [a]) -> [a]
sortJoin :: (a -> a -> Bool) -> [a] -> [a] -> [a]
sortJoin_ :: (a -> a -> Bool) -> ([a], [a]) -> [a] -> [a]
sortMerge _ [] =
[]
sortMerge _ [a] =
[a]
sortMerge funCompare list =
sortMerge_ funCompare $ splitAt ((length list) `div` 2) list
sortMerge_ _ ([], b) =
b
sortMerge_ funCompare (a, b) =
sortJoin funCompare (sortMerge funCompare a) (sortMerge funCompare b)
sortJoin _ a [] =
a
sortJoin funCompare a b =
sortJoin_ funCompare (partition (\x ->
funCompare x $ head b
) a
) b
sortJoin_ funCompare (a1, a2) b =
a1 ++ [(head b)] ++ (sortJoin funCompare a2 $ tail b) |
[ 本帖最后由 Magicloud 于 2008-12-5 13:14 编辑 ] |
|