A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
17
Task/Middle-three-digits/Haskell/middle-three-digits.hs
Normal file
17
Task/Middle-three-digits/Haskell/middle-three-digits.hs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import Numeric
|
||||
|
||||
mid3 :: Integral a => a -> Either String String
|
||||
mid3 n | m < 100 = Left "is too small"
|
||||
| even l = Left "has an even number of digits"
|
||||
| otherwise = Right . take 3 $ drop ((l-3) `div` 2) s
|
||||
where m = abs n
|
||||
s = showInt m ""
|
||||
l = length s
|
||||
|
||||
showMid3 :: Integer -> String
|
||||
showMid3 n = show n ++ ": " ++ either id id (mid3 n)
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ (putStrLn . showMid3) [
|
||||
123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345,
|
||||
1, 2, -1, -10, 2002, -2002, 0]
|
||||
Loading…
Add table
Add a link
Reference in a new issue