I'm trying to sort 3 elements in a list. But i"m having trouble translating it to haskell. Is it possible to do nested if statements in haskell? I've been trying pattern matching, but it is taking me forever.
if (x < y) {
if (z < x) swap(x,z);
} else {
if (y < z) swap(x,y);
else swap(x,z);
}
if(z<y) swap(y,z);
this is what I have tried
intCMP :: Int -> Int -> Ordering
intCMP a b | a == b =EQ
| a < b = LT
| otherwise = GT
sort3 :: Ord a => (a -> a -> Ordering) -> [a] -> [a]
sort3 cmp [a,b,c] = if cmp a b == LT then
if cmp a c == Lt then
if cmp b c == LT then
[a,b,c]
else
[a,c,b]
else
[c,a,b]
else if cmp b c == LT then
if cmp a c == LT then
[b,a,c]
else
[b,c,a]
else
[c,b,a]
LtforLT) that code works just fine when I try it.MultiWayIfextension