Middle three digits: Difference between revisions
Content deleted Content added
m →{{header|JavaScript}}: (simpler expression for max numeric string length) |
m →{{header|Haskell}}: (Aligned the output cols for easier reading) |
||
Line 2,452:
mid3 n
| m < 100 = Left "is too small"
| even
| otherwise = Right . take 3 $ drop ((
where
m = abs n
s = show m
-- TEST --------------------------------------------------------
main :: IO ()
main = do
let xs =
,
,
,
, -10001
, -
, -
,
,
,
,
,
, -
,
, -2002
,
w = maximum $ (length . show) <$> xs
]</lang>▼
(putStrLn . unlines) $
justifyRight w ' ' (show n) ++
" -> " ++ either ((>>= id) . ("(" :) . (: [")"])) id (mid3 n)) <$>
xs
where
justifyRight :: Int -> Char -> String -> String
justifyRight n c s = drop (length s) (replicate n c ++ s)</lang>
Output:
<pre> 123
12345
1234567
987654321
10001
-10001
-123
-100
100
-12345
1
2
-1
-10
2002
-2002
0
=={{header|Icon}} and {{header|Unicon}}==
|