Sunday, September 7, 2008

Triangle verification in Haskell

As promised here is my first code snippet. This code can tell you whatever 3 numbers form a valid triangle or not, and if they do it also tells you what kind of triangle.


max3 , min3, middle :: Int -> Int -> Int -> Int

max3 a b c = max (max a b) c

min3 a b c = min (min a b) c

middle a b c = max (min a b) (min b c)

sort3 :: Int -> Int -> Int -> (Int,Int,Int)

sort3 a b c = ((max3 a b c), (middle a b c), (min3 a b c))

data Triangle = Failure | Isosceles | Equilateral | Scalene
deriving(Show)

analyse :: (Int,Int,Int) -> Triangle
analyse (a,b,c)
| a >= b + c = Failure
| b == c = Equilateral
| (a == b) && (b == a) = Isosceles
| otherwise = Scalene

safe_analyse :: Int -> Int -> Int -> Triangle
safe_analyse a b c = analyse (sort3 a b c)

No comments: