Sort tuple (a, a, a)

0

Hello, I have the following tuple:

ordena :: Ord a => (a,a,a) -> (a,a,a)

to see this does not have much complication but as I do I get a very ugly and long code.

In this case I ask to see if there is some kind of recursion that I can do to order it from lowest to highest

    
asked by Ramosaurio 16.10.2016 в 11:41
source

2 answers

2

Without using any module (as I suppose you want), a simple and straightforward solution would be:

ordena :: Ord a => (a,a,a) -> (a,a,a)
ordena (x,y,z) | x > y     = ordena (y,x,z)
               | y > z     = ordena (x,z,y)
               | otherwise = (x,y,z)
    
answered by 18.10.2016 / 01:04
source
1

You can convert the tuple into a list, sort the list, and return the tuple:

import Data.List(sort)
ordena :: Ord a => (a,a,a) -> (a,a,a)
ordena (a,b,c) = let [d,e,f] = sort [a,b,c] in (d,e,f)
    
answered by 16.10.2016 в 17:27