[Haskell-beginners] do notation, pattern matching, if, & case

Vale Cofer-Shabica vale.cofershabica at gmail.com
Tue Aug 26 18:16:45 UTC 2014


I'm working on a cli program where I'd like to take input from a file
or stdin. I've managed to get my program to work but thought my
dispatch mechanism could be simplified. I use a scheme along the lines
of f in the following minimal example, which compiles. However, if I
try to use f' or f'', I get parse errors. Three questions:

* Is there a better way of doing this
* Are stylistic changes to f really called for?
* How can/ought I correct my syntax errors?

Many thanks,
vale

>module MinimalExample where
>import System.IO (getContents)

>f :: String -> IO ()
>f "-" = do
>  s <- getContents
>  putStrLn s
>f file = do
>  s <- readFile file
>  putStrLn s

>-- f' :: String -> IO ()
>-- f' file = do
>--   if file == "-"
>--     then s <- getContents
>--     else s <- readFile file
>--   putStrLn s

>-- f'' :: String -> IO ()
>-- f'' file = do
>--   case file of
>--     "-" -> s <- getContents
>--     _ -> s <- readFile file
>--   putStrLn s


--
vale cofer-shabica
vale.cofershabica at gmail.com


More information about the Beginners mailing list