Haskell经典算法和问题搜集

来源:互联网 发布:易语言编程 编辑:程序博客网 时间:2024/06/05 10:29

快速排序

quick_sort [] = []quick_sort (x:[]) = [x]quick_sort (x:xs) =    let smaller_or_equal_list = [a| a<-xs, a<=x]        larger_list = [a| a<-xs, a>x]    in quick_sort smaller_or_equal_list ++ [x] ++ quick_sort larger_list

插入排序

iSort :: [Integer] -> [Integer]iSort [] = []iSort (x:xs) = ins x $ iSort xsins :: Integet -> [Integer] -> [Integer]ins x [] = [x]ins x (y:ys)  | x <= y = x : y : ys  | otherwise = y : ins x ys

八皇后

main  = print $ queens 8boardSize = 8queens 0 = [[]]queens n = [ x : y | y <- queens (n-1), x <- [1..boardSize], safe x y 1]      where         safe x [] n = True         safe x (c:y) n = and [ x /= c , x /= c + n , x /= c - n , safe x y (n+1)]
0 0