about summary refs log tree commit diff
path: root/scratch/haskell-programming-from-first-principles/io.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-17T22·41+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-17T22·41+0100
commita2475d2337df6a91af2eb353d174f5202f1207b4 (patch)
treeb4dec0827d132b99244ec74dbeac450d2db040bd /scratch/haskell-programming-from-first-principles/io.hs
parentca06888a568b4b5de14a72c801e8cff96fb5260d (diff)
Partially complete IO chapter exercises
Inconveniently, I do not have the cipher code that I wrote from a previous
chapter, and I'm not eager to reimplement it.

TODO
- Implement encrypt
- Implement decrypt
- Read all characters from STDIN
Diffstat (limited to 'scratch/haskell-programming-from-first-principles/io.hs')
-rw-r--r--scratch/haskell-programming-from-first-principles/io.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/scratch/haskell-programming-from-first-principles/io.hs b/scratch/haskell-programming-from-first-principles/io.hs
new file mode 100644
index 000000000000..1de8937fced4
--- /dev/null
+++ b/scratch/haskell-programming-from-first-principles/io.hs
@@ -0,0 +1,35 @@
+module IOScratch where
+
+import qualified System.Environment as SE
+import qualified System.IO as SIO
+--------------------------------------------------------------------------------
+
+docs :: String
+docs = "Pass -e to encrypt and -d to decrypt."
+
+encryptStdin :: IO ()
+encryptStdin = do
+  char <- SIO.hGetChar SIO.stdin
+  -- encrypt char
+  SIO.hPutStr SIO.stdout [char]
+
+decryptStdin :: IO ()
+decryptStdin = do
+  char <- SIO.hGetChar SIO.stdin
+  -- decrypt char
+  SIO.hPutStr SIO.stdout [char]
+
+main :: IO ()
+main = do
+  args <- SE.getArgs
+  case args of
+    [] ->
+      putStrLn $ "You did not pass enough arguments. " ++ docs
+    ["-e"] ->
+      encryptStdin
+    ["-d"] ->
+      decryptStdin
+    [x] ->
+      putStrLn $ "You passed an unsupported option: " ++ x ++ ". " ++ docs
+    _ ->
+      putStrLn $ "You passed too many arguments. " ++ docs