never executed always true always false
1 module Main where
2
3 reciprocal :: Int -> (String, Int)
4 reciprocal n | n > 1 = ('0' : '.' : digits, recur)
5 | otherwise = error
6 "attempting to compute reciprocal of number <= 1"
7 where
8 (digits, recur) = divide n 1 []
9 divide :: Int -> Int -> [Int] -> (String, Int)
10 divide n c cs | c `elem` cs = ([], position c cs)
11 | r == 0 = (show q, 0)
12 | r /= 0 = (show q ++ digits, recur)
13 where
14 (q, r) = (c*10) `quotRem` n
15 (digits, recur) = divide n r (c:cs)
16
17 position :: Int -> [Int] -> Int
18 position n (x:xs) | n==x = 1
19 | otherwise = 1 + position n xs
20
21 showRecip :: Int -> String
22 showRecip n =
23 "1/" ++ show n ++ " = " ++
24 if r==0 then d else take p d ++ "(" ++ drop p d ++ ")"
25 where
26 p = length d - r
27 (d, r) = reciprocal n
28
29 main = do
30 number <- readLn
31 putStrLn (showRecip number)
32 main
33