Search code examples
haskellintguard

How do I return the middle number in Haskell


I have the following beginning of a function, and am unsure as to how I should return Middle Number (i.e. the number that is neither the largest nor smallest):

middleNumber :: Int -> Int -> Int -> Int
middleNumber a b c
    | ...

Solution

  • I would recommend you break the function into two steps: First, sort the three numbers. Then, take the middle element. For the first step, also consider if you can take it one step at a time; each step bringing it a bit closer to being fully sorted, then tail-recursing back to bring it even closer.