-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathisFileInputPrime.hs
More file actions
30 lines (25 loc) · 845 Bytes
/
isFileInputPrime.hs
File metadata and controls
30 lines (25 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import System.Environment
import System.IO
isPrime :: Int -> Bool
isPrime n
| n <= 1 = False
| otherwise = not $ any (\x -> n `mod` x == 0) [2,3..(n-1)]
handleLine :: String -> Maybe Bool
handleLine numberString =
if length numberString == 0
then Nothing
else let number = read numberString :: Int
in Just (isPrime number)
--printLine :: String -> IO ()
printLine line = do
case handleLine line of
(Just x) -> putStrLn line >> print x
Nothing -> return ()
main :: IO ()
main = do
(filename:_) <- getArgs
handle <- openFile filename ReadMode
hSetBuffering handle LineBuffering
contents <- hGetContents handle
mapM_ printLine (lines contents)
return ()