churf/sample-programs/Quicksort.crf

44 lines
1,023 B
Text

toStr : List Int -> List Char
toStr xs = case xs of
Cons a as => Cons (toChar a) (toStr as)
Nil => Nil
toChar : Int -> Char
toChar x = case x of
0 => '0'
1 => '1'
2 => '2'
3 => '3'
4 => '4'
5 => '5'
6 => '6'
7 => '7'
8 => '8'
9 => '9'
filter p xs = case xs of
Nil => Nil
Cons x xs => case p x of
True => Cons x (filter p xs)
False => filter p xs
.++ as bs = case as of
Nil => bs
Cons x xs => Cons x (xs ++ bs)
.:: a as = Cons a as
quicksort : List Int -> List Int
quicksort xs = case xs of
Nil => Nil
Cons a as => let smaller = quicksort (filter (\y. y < a) xs)
in let bigger = quicksort (filter (\y. a < y) xs)
in smaller ++ (a :: bigger)
descList : Int -> Int -> List Int
descList from to = case to < from of
False => Cons to (descList from (to - 1))
True => Nil
main = let list = (5 :: (2 :: (8 :: (9 :: (6 :: (0 :: (1 :: Nil)))))))
in printStr (toStr (quicksort list))