-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytestring.hs
67 lines (57 loc) · 2.02 KB
/
bytestring.hs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString as S
import System.Environment
import System.IO
import System.IO.Error
{--fch = B.fromChunks [S.pack [40,41,42], S.pack [43,44,45], S.pack [46,47,48]]
B.cons 85 $ B.pack [80,81,82,84]
B.cons' 85 $ B.pack [80,81,82,84]
foldr B.cons B.empty [50..60]
Chunk "2" (Chunk "3" (Chunk "4" (Chunk "5" (Chunk "6" (Chunk "7" (Chunk "8" (Chunk "9" (Chunk ":" (Chunk ";" (Chunk "<"
Empty))))))))))
foldr B.cons' B.empty [50..60]
Chunk "23456789:;<" Empty
--}
main1 = do
(fileName1:fileName2:_) <- getArgs
copyFile fileName1 fileName2
copyFile :: FilePath -> FilePath -> IO ()
copyFile source dest = do
contents <- B.readFile source
B.writeFile dest contents
main = toTry `catch` handler
toTry :: IO ()
toTry = do (fileName:_) <- getArgs
contents <- readFile fileName
putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!"
handler :: IOError -> IO ()
handler e
| isDoesNotExistError e = putStrLn "The file doesn't exist!"
| otherwise = ioError e
{--
get attributes form exception
handler :: IOError -> IO ()
handler e
| isDoesNotExistError e = case ioeGetFileName e of
Just path -> putStrLn $ "Whoops! File does not exist at: " ++ path
Nothing -> putStrLn "Whoops! File does not exist at unknown location!"
| otherwise = ioError e
--}
{--The predicates that act on IOError are:
isAlreadyExistsError
isDoesNotExistError
isAlreadyInUseError
isFullError
isEOFError
isIllegalOperation
isPermissionError
isUserError
---
isUserError evaluates to True when we use the function
userError to make the exception, which is used for making
exceptions from our code and equipping them with a string.
For instance, you can do ioError $ userError "remote computer
unplugged!", although It's prefered you use types like Either
and Maybe to express possible failure instead of throwing
exceptions yourself with userError.
--}