Friday, 3 June 2016

Haskell: Find last digit of a number


lastDigit.hs
{- Return the last digit of a number -}
getLastDigit :: Integer -> Integer
getLastDigit num
    | (num < 0) = ((-1) * num) `rem` 10
    | otherwise = num `rem` 10

*Main> :load lastDigit.hs
[1 of 1] Compiling Main             ( lastDigit.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> getLastDigit (-1234)
4
*Main> 
*Main> getLastDigit 1234
4


Previous                                                 Next                                                 Home

No comments:

Post a Comment