haskell - Recursion error while sorting a list of numbers without quicksort -
order :: [int] -> [int] order [] = [] order [x] = [x] order (x:y:xs) = if x>y [y] ++ (order (x:xs)) else [x] ++ (order (y:xs))
i tried use haskell code sort list of numbers, when input list more 4 elements doesn't sort them correctly. want keep code compiles don't know how make recursion work correctly.
the code fine , implemented, implemented not in fact sort list. let's try [4,3,2]
:
order [4,3,2] order [] = [] -- nope not empty list order [x] = [x] -- nope not singleton order (x:y:xs) = if x>y -- x=4, y=3, xs =[2]; x >y true [y] ++ (order (x:xs)) -- branch [3] ++ (order [4,2]) else [x] ++ (order (y:xs)) -- not branch
so [3] ++ (order [4,2])
. question you: how 2
move other side of 3
?
Comments
Post a Comment