-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathisFileInputPrime_outToFile.hs
More file actions
33 lines (28 loc) · 1003 Bytes
/
isFileInputPrime_outToFile.hs
File metadata and controls
33 lines (28 loc) · 1003 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
30
31
32
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 handle line = do
case handleLine line of
(Just x) -> hPutStrLn handle line >> hPrint handle x
Nothing -> return ()
main :: IO ()
main = do
(inFile:outFile:_) <- getArgs
inHandle <- openFile inFile ReadMode
outHandle <- openFile outFile WriteMode
hSetBuffering inHandle LineBuffering
contents <- hGetContents inHandle
mapM_ (\x -> printLine outHandle x) (lines contents)
hClose inHandle
hClose outHandle
return ()