stefan@stefans-MacBook-Pro curs7 % ghc curs7.hs ghc curs7.hs [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 ./curs7 Hello! stefan@stefans-MacBook-Pro curs7 % ghci ghci GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help Prelude> :i IO :i IO type IO :: * -> * newtype IO a = GHC.Types.IO (GHC.Prim.State# GHC.Prim.RealWorld -> (# GHC.Prim.State# GHC.Prim.RealWorld, a #)) -- Defined in ‘GHC.Types’ instance Applicative IO -- Defined in ‘GHC.Base’ instance Functor IO -- Defined in ‘GHC.Base’ instance Monad IO -- Defined in ‘GHC.Base’ instance Monoid a => Monoid (IO a) -- Defined in ‘GHC.Base’ instance Semigroup a => Semigroup (IO a) -- Defined in ‘GHC.Base’ instance MonadFail IO -- Defined in ‘Control.Monad.Fail’ Prelude> :t putStrLn :t putStrLn putStrLn :: String -> IO () Prelude> :q :q Leaving GHCi. stefan@stefans-MacBook-Pro curs7 % ls -al ls -al total 2648 drwxr-xr-x 6 stefan staff 192 Apr 6 18:18 . drwxr-xr-x 14 stefan staff 448 Apr 6 17:54 .. -rwxr-xr-x 1 stefan staff 1342328 Apr 6 17:55 curs7 -rw-r--r-- 1 stefan staff 669 Apr 6 17:55 curs7.hi -rw-r--r-- 1 stefan staff 942 Apr 6 18:18 -rw-r--r-- 1 stefan staff 2464 Apr 6 17:55 curs7.o stefan@stefans-MacBook-Pro curs7 % cat curs7.hs cat curs7.hs -- Monada IO -- modalitate de a scrie programe -- de facto imperative intr-un cadru pur functional -- IO este un tip de date parametrizat -- valorile de tip "IO a": -- comenzi/actiuni care, daca le executam pe calculator, -- produc la sfarsit o valoare de tip "a". -- ^^^^^^ -- actiunile produc o valoare -- -nu spun returneaza- (fiindca functiiile returneaza) -- putStrLn :: String -> IO () -- primeste la intrare un sir de caractere si -- intoarce o actiune care, daca este executata, produce o valoare -- ^^^^^^^^^^^^^^^^^^^ -- afiseze sirul de caractere -- de tip () (citesc: "de tip unit"). -- () :: () (exista o singura valoare, (), de tip ()) -- daca definesc o functie "main" de tip "IO *", -- compilatorul GHC construieste un executabil care: -- 1. evalueaza functia main si calculeaza actiunea -- 2. executa actiunea main = putStrLn "Hello!" stefan@stefans-MacBook-Pro curs7 % ghc curs7.hs ghc curs7.hs [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ls ls curs7 curs7.hi curs7.hs curs7.o stefan@stefans-MacBook-Pro curs7 % head curs7.hi head curs7.hi 81073物趗٤ⴇűԾ)딝/̶ƌNߵܓǵ̈B  ׷kĂ\ȣfÝ&ɖΉoڮ stefan@stefans-MacBook-Pro curs7 % ./curs7 ./curs7 Hello! stefan@stefans-MacBook-Pro curs7 % ghc curs7 ghc curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ghci ghci GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help Prelude> :l curs7 :l curs7 [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main Hello! *Main> :t (>>) :t (>>) (>>) :: Monad m => m a -> m b -> m b *Main> :i IO :i IO type IO :: * -> * newtype IO a = GHC.Types.IO (GHC.Prim.State# GHC.Prim.RealWorld -> (# GHC.Prim.State# GHC.Prim.RealWorld, a #)) -- Defined in ‘GHC.Types’ instance Applicative IO -- Defined in ‘GHC.Base’ instance Functor IO -- Defined in ‘GHC.Base’ instance Monad IO -- Defined in ‘GHC.Base’ instance Monoid a => Monoid (IO a) -- Defined in ‘GHC.Base’ instance Semigroup a => Semigroup (IO a) -- Defined in ‘GHC.Base’ instance MonadFail IO -- Defined in ‘Control.Monad.Fail’ *Main> :t (>>) :t (>>) (>>) :: Monad m => m a -> m b -> m b *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main Hello! World! *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main Hello! World! *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main Hello! World! *Main> :r :r Ok, one module loaded. *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main Hello! World! *Main> :t (>>) :t (>>) (>>) :: Monad m => m a -> m b -> m b *Main> :t (>>=) :t (>>=) (>>=) :: Monad m => m a -> (a -> m b) -> m b *Main> :t getLine :t getLine getLine :: IO String *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main Hello! Everyone! World! *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main What it your name? Stefan Stefan Hello, asdf! *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main What it your name? Stefan Stefan Hello, Stefan! *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) curs7.hs:53:8: error: • Couldn't match type ‘Char’ with ‘()’ Expected type: IO () Actual type: IO Char • In the expression: putStrLn "What it your name?" >> (getLine >>= (\ x -> putStrLn "Hello, " ++ x ++ "!")) In an equation for ‘main’: main = putStrLn "What it your name?" >> (getLine >>= (\ x -> putStrLn "Hello, " ++ x ++ "!")) | 53 | main = putStrLn "What it your name?" >> | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^... curs7.hs:54:28: error: • Couldn't match expected type ‘[Char]’ with actual type ‘IO ()’ • In the first argument of ‘(++)’, namely ‘putStrLn "Hello, "’ In the expression: putStrLn "Hello, " ++ x ++ "!" In the second argument of ‘(>>=)’, namely ‘(\ x -> putStrLn "Hello, " ++ x ++ "!")’ | 54 | (getLine >>= (\x -> putStrLn "Hello, " ++ x ++ "!")) | ^^^^^^^^^^^^^^^^^^ curs7.hs:54:28: error: • Couldn't match type ‘[]’ with ‘IO’ Expected type: IO Char Actual type: [Char] • In the expression: putStrLn "Hello, " ++ x ++ "!" In the second argument of ‘(>>=)’, namely ‘(\ x -> putStrLn "Hello, " ++ x ++ "!")’ In the second argument of ‘(>>)’, namely ‘(getLine >>= (\ x -> putStrLn "Hello, " ++ x ++ "!"))’ | 54 | (getLine >>= (\x -> putStrLn "Hello, " ++ x ++ "!")) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed, no modules loaded. Prelude> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main What it your name? Stefan Stefan Hello, Stefan! *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main What it your name? Stefan Stefan Hello, Stefan! *Main> :r :r Ok, one module loaded. *Main> main main What it your name? asdadsa asdadsa Hello, asdadsa! *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) curs7.hs:72:8: error: Unexpected do block in function application: do putStrLn "What it your name?" You could write it with parentheses Or perhaps you meant to enable BlockArguments? | 72 | main = do putStrLn "What it your name?" | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ curs7.hs:73:11: error: parse error on input ‘<-’ Perhaps this statement should be within a 'do' block? | 73 | x <- getLine | ^^ Failed, no modules loaded. Prelude> let x = getLine let x = getLine Prelude> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) curs7.hs:81:34: error: • Couldn't match expected type ‘[Char]’ with actual type ‘IO String’ • In the first argument of ‘(++)’, namely ‘x’ In the second argument of ‘(++)’, namely ‘x ++ "!"’ In the first argument of ‘putStrLn’, namely ‘("Hello, " ++ x ++ "!")’ | 81 | putStrLn ("Hello, " ++ x ++ "!") | ^ Failed, no modules loaded. Prelude> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) curs7.hs:82:13: error: parse error (possibly incorrect indentation or mismatched brackets) | 82 | (\y -> putStrLn ("Hello, " ++ y ++ "!")) | ^ Failed, no modules loaded. Prelude> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main What it your name? Stefan Stefan Hello, Stefan! *Main> :t return :t return return :: Monad m => a -> m a *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) curs7.hs:88:1: error: Duplicate type signatures for ‘main’ at curs7.hs:78:1-4 curs7.hs:88:1-4 | 88 | main :: IO () | ^^^^ curs7.hs:89:1: error: Multiple declarations of ‘main’ Declared at: curs7.hs:79:1 curs7.hs:89:1 | 89 | main = do putStrLn "Hello! What is your name?" | ^^^^ Failed, no modules loaded. Prelude> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main Hello! What is your name? Stefan Stefan Nice to meet you, Stefan! Hello! What is your name? Asdf Asdf Nice to meet you, Asdf! Hello! What is your name? 123 123 Nice to meet you, 123! Hello! What is your name? *Main> :R :R unknown command ':R' use :? for help. *Main> :R :R unknown command ':R' use :? for help. *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) curs7.hs:103:1: error: Duplicate type signatures for ‘main’ at curs7.hs:88:1-4 curs7.hs:103:1-4 | 103 | main :: IO () | ^^^^ curs7.hs:104:1: error: Multiple declarations of ‘main’ Declared at: curs7.hs:89:1 curs7.hs:104:1 | 104 | main = do return () | ^^^^ Failed, no modules loaded. Prelude> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main Hello! What is your name? *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) curs7.hs:108:1: error: Duplicate type signatures for ‘main’ at curs7.hs:103:1-4 curs7.hs:108:1-4 | 108 | main :: IO () | ^^^^ curs7.hs:109:1: error: Multiple declarations of ‘main’ Declared at: curs7.hs:104:1 curs7.hs:109:1 | 109 | main = return () >> putStrLn "Hello! What is your name?" | ^^^^ Failed, no modules loaded. Prelude> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> main main Hello! What is your name? *Main> :t getArgs :t getArgs :1:1: error: Variable not in scope: getArgs *Main> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) curs7.hs:134:1: error: parse error (possibly incorrect indentation or mismatched brackets) Failed, no modules loaded. Prelude> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main> :t getArgs :t getArgs :1:1: error: Variable not in scope: getArgs *Main> import System.IO import System.IO *Main System.IO> :t getArgs :t getArgs :1:1: error: Variable not in scope: getArgs *Main System.IO> import System.Args import System.Args : error: Could not find module ‘System.Args’ It is not a module in the current program, or in any known package. *Main System.IO> import System.Arguments import System.Arguments : error: Could not find module ‘System.Arguments’ It is not a module in the current program, or in any known package. *Main System.IO> import System.Environment import System.Environment *Main System.IO System.Environment> :t getArgs :t getArgs getArgs :: IO [String] *Main System.IO System.Environment> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main System.IO System.Environment> :t getArgs :t getArgs getArgs :: IO [String] *Main System.IO System.Environment> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main System.IO System.Environment> :q :q Leaving GHCi. stefan@stefans-MacBook-Pro curs7 % ghc curs7 ghc curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 asdf 123 ./curs7 asdf 123 ["asdf","123"] stefan@stefans-MacBook-Pro curs7 % ghc curs7 ghc curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 ./curs7 "curs7" [] stefan@stefans-MacBook-Pro curs7 % ./curs7 asdf 123 14 ./curs7 asdf 123 14 "curs7" ["asdf","123","14"] stefan@stefans-MacBook-Pro curs7 % ghc curs7 ghc curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 asdf 123 14 ./curs7 asdf 123 14 curs7 ["asdf","123","14"] stefan@stefans-MacBook-Pro curs7 % ghc curs7 ghc curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 asdf 123 14 ./curs7 asdf 123 14 asdf 123 14 stefan@stefans-MacBook-Pro curs7 % ghc curs7 ghc curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 curs7.hs ./curs7 curs7.hs Citesc fisierul curs7.hs stefan@stefans-MacBook-Pro curs7 % ghci ghci GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help Prelude> :m + System.IO :m + System.IO Prelude System.IO> :t openFile :t openFile openFile :: FilePath -> IOMode -> IO Handle Prelude System.IO> :t FilePath :t FilePath :1:1: error: Data constructor not in scope: FilePath Prelude System.IO> :i FilePath :i FilePath type FilePath :: * type FilePath = String -- Defined in ‘GHC.IO’ Prelude System.IO> :i IOMode :i IOMode type IOMode :: * data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode -- Defined in ‘GHC.IO.IOMode’ instance Eq IOMode -- Defined in ‘GHC.IO.IOMode’ instance Ord IOMode -- Defined in ‘GHC.IO.IOMode’ instance Enum IOMode -- Defined in ‘GHC.IO.IOMode’ instance Show IOMode -- Defined in ‘GHC.IO.IOMode’ instance Read IOMode -- Defined in ‘GHC.IO.IOMode’ Prelude System.IO> :q :q Leaving GHCi. stefan@stefans-MacBook-Pro curs7 % ghc curs7.hs && ./curs7 ghc curs7.hs && ./curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) curs7.hs:167:11: error: • Couldn't match type ‘Handle’ with ‘()’ Expected type: IO () Actual type: IO Handle • In a stmt of a 'do' block: openFile filename ReadMode In the expression: do (filename : _) <- getArgs openFile filename ReadMode In an equation for ‘main’: main = do (filename : _) <- getArgs openFile filename ReadMode | 167 | openFile filename ReadMode | ^^^^^^^^^^^^^^^^^^^^^^^^^^ stefan@stefans-MacBook-Pro curs7 % ghci ghci GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help Prelude> :t (>>) :t (>>) (>>) :: Monad m => m a -> m b -> m b Prelude> :m + System.IO :m + System.IO Prelude System.IO> readLines readLines :3:1: error: Variable not in scope: readLines Prelude System.IO> :t readLine :t readLine :1:1: error: • Variable not in scope: readLine • Perhaps you meant one of these: ‘readFile’ (imported from Prelude), ‘readLn’ (imported from Prelude), ‘readList’ (imported from Prelude) Prelude System.IO> :t readFile :t readFile readFile :: FilePath -> IO String Prelude System.IO> :t readContents :t readContents :1:1: error: • Variable not in scope: readContents • Perhaps you meant ‘getContents’ (imported from Prelude) Prelude System.IO> :t hGetContents :t hGetContents hGetContents :: Handle -> IO String Prelude System.IO> :i Handle :i Handle type Handle :: * data Handle = GHC.IO.Handle.Types.FileHandle FilePath {-# UNPACK #-}(GHC.MVar.MVar GHC.IO.Handle.Types.Handle__) | GHC.IO.Handle.Types.DuplexHandle FilePath {-# UNPACK #-}(GHC.MVar.MVar GHC.IO.Handle.Types.Handle__) {-# UNPACK #-}(GHC.MVar.MVar GHC.IO.Handle.Types.Handle__) -- Defined in ‘GHC.IO.Handle.Types’ instance Eq Handle -- Defined in ‘GHC.IO.Handle.Types’ instance Show Handle -- Defined in ‘GHC.IO.Handle.Types’ Prelude System.IO> :q :q Leaving GHCi. stefan@stefans-MacBook-Pro curs7 % ghc ./curs7.hs && ./curs7 ghc ./curs7.hs && ./curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... curs7: user error (Pattern match failure in do expression at curs7.hs:166:11-22) stefan@stefans-MacBook-Pro curs7 % ghc ./curs7.hs && ./curs7 curs7.hs ghc ./curs7.hs && ./curs7 curs7.hs import System.Environment import System.IO -- Monada IO -- modalitate de a scrie programe -- de facto imperative intr-un cadru pur functional -- IO este un tip de date parametrizat -- valorile de tip "IO a": -- comenzi/actiuni care, daca le executam pe calculator, -- produc la sfarsit o valoare de tip "a". -- ^^^^^^ -- actiunile produc o valoare -- -nu spun returneaza- (fiindca functiiile returneaza) -- putStrLn :: String -> IO () -- primeste la intrare un sir de caractere si -- intoarce o actiune care, daca este executata, produce o valoare -- ^^^^^^^^^^^^^^^^^^^ -- afiseze sirul de caractere -- de tip () (citesc: "de tip unit"). -- () :: () (exista o singura valoare, (), de tip ()) -- daca definesc o functie "main" de tip "IO *", -- compilatorul GHC construieste un executabil care: -- 1. evalueaza functia main si calculeaza actiunea -- 2. executa actiunea -- main :: IO () -- main = putStrLn "Hello!" -- Functia (>>) -- main = (>>) (putStrLn "Hello!") (putStrLn "World!") -- main :: IO () -- main = putStrLn "Hello!" >> -- putStrLn "Everyone!" >> -- putStrLn "World!" -- getLine :: IO String -- getLine este o actiune care asteapta un input de la tastatura -- si la sfarsit produce ca rezultat sirul de caractere introdus -- la intrarea standard -- main :: IO () -- main = putStrLn "What it your name?" >> -- (getLine >>= (\x -> putStrLn ("Hello, " ++ x ++ "!"))) -- main :: IO () -- main = putStrLn "What it your name?" >> -- getLine >>= -- \x -> putStrLn ("Hello, " ++ x ++ "!") -- zahar sintactic (notatia "do", "do" notation) -- main :: IO () -- main = do putStrLn "What it your name?" -- x <- getLine -- putStrLn ("Hello, " ++ x ++ "!") -- daca nu vreau sa fiu indentation sensitive: -- main :: IO () -- main = do { putStrLn "What it your name?" ; -- x <- getLine ; -- putStrLn ("Hello, " ++ x ++ "!") } -- nu merge daca gresesc indentarea -- main :: IO () -- main = do putStrLn "What it your name?" -- x <- getLine -- putStrLn ("Hello, " ++ x ++ "!") -- Vreau sa nu confund x <- getLine, care este o notatie, -- cu let x = getLine. -- main :: IO () -- main = do putStrLn "What it your name?" -- let x = getLine in -- x >>= -- (\y -> putStrLn ("Hello, " ++ y ++ "!")) -- cand scriu let x = getLine, x devine un sinonim pentru getLine -- cum obtinem comportamente repetitive -- main :: IO () -- main = do putStrLn "Hello! What is your name?" -- x <- getLine -- if x == "" then -- -- actiunea pe care o intoarce main daca x este sirul vid -- return () -- else -- -- actiunea pe care o intoarce main daca x nu este sirul vid -- do putStrLn ("Nice to meet you, " ++ x ++ "!") -- main -- return x este o actiunea care nu efectueaza nimic -- si la sfarsit produce valoarea x -- Atentie! nu este acelasi lucru cu return-ul din C!!! -- main :: IO () -- main = do return () -- putStrLn "Hello! What is your name?" -- este sintactic sugar pentru -- main :: IO () -- main = return () >> putStrLn "Hello! What is your name?" -- (G, +, ...) este un grup daca ... -- (M, >>=, return) este o monada -- daca respecta anumite legi. -- de facto, functiile care intorc actiuni IO sunt programe imperative -- cand scriu un program mare: -- prefer cat mai multe functii sa fie functii pure -- si cat mai putine in monada IO -- O intrebare frecventa: -- am functia getLine :: IO String -- cum scriu getLine :: String? -- Raspuns: nu se poate!! -- cum "punem mana" pe argumentele din linia de comanda -- main :: IO () -- main = do xs <- getArgs -- print xs -- main :: IO () -- main = do xs <- getArgs -- name <- getProgName -- putStrLn name -- print xs -- main :: IO () -- main = do xs <- getArgs -- name <- getProgName -- putStrLn name -- print xs -- proceseaza :: [String] -> IO () -- proceseaza [] = return () -- proceseaza (x:xs) = do putStrLn x -- proceseaza xs -- main :: IO () -- main = do xs <- getArgs -- proceseaza xs -- cum interactioneaza evaluarea lenesa cu monada IO ? -- facem un program care citeste un fisier sursa -- Haskell si scriem la iesirea std fara comentarii (linii care incep cu --) main :: IO () main = do (filename:_) <- getArgs handle <- openFile filename ReadMode continut <- hGetContents handle putStrLn continut stefan@stefans-MacBook-Pro curs7 % ghci ghci GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help Prelude> :m + System.IO :m + System.IO Prelude System.IO> hIsEOF hIsEOF :2:1: error: • No instance for (Show (Handle -> IO Bool)) arising from a use of ‘print’ (maybe you haven't applied a function to enough arguments?) • In a stmt of an interactive GHCi command: print it Prelude System.IO> :t hIsEOF :t hIsEOF hIsEOF :: Handle -> IO Bool Prelude System.IO> :t hGetLine :t hGetLine hGetLine :: Handle -> IO String Prelude System.IO> :q :q Leaving GHCi. stefan@stefans-MacBook-Pro curs7 % ghc curs7 ghc curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 test.hs ./curs7 test.hs import System.Environment stefan@stefans-MacBook-Pro curs7 % cat test.hs cat test.hs import System.Environment import System.IO -- Monada IO main :: IO () main = putStrLn "Hello!" -- Functia (>>) -- main = (>>) (putStrLn "Hello!") (putStrLn "World!") stefan@stefans-MacBook-Pro curs7 % ./curs7 test.hs ./curs7 test.hs import System.Environment stefan@stefans-MacBook-Pro curs7 % ghc curs7.hs ghc curs7.hs [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 test.hs ./curs7 test.hs import System.Environment import System.IO -- Monada IO main :: IO () main = putStrLn "Hello!" -- Functia (>>) -- main = (>>) (putStrLn "Hello!") (putStrLn "World!") stefan@stefans-MacBook-Pro curs7 % ghc curs7 ghc curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 test.hs ./curs7 test.hs import System.Environment import System.IO main :: IO () main = putStrLn "Hello!" stefan@stefans-MacBook-Pro curs7 % ./curs7 curs.hs ./curs7 curs.hs curs7: curs.hs: openFile: does not exist (No such file or directory) stefan@stefans-MacBook-Pro curs7 % ./curs7 curs7.hs ./curs7 curs7.hs import System.Environment import System.IO isComment :: String -> Bool isComment ('-':'-':_) = True isComment _ = False -- aici e partea interesanta proceseaza :: Handle -> IO () proceseaza handle = do b <- hIsEOF handle if b then return () else do line <- hGetLine handle if isComment line then return () else putStrLn line proceseaza handle main :: IO () main = do (filename:_) <- getArgs handle <- openFile filename ReadMode proceseaza handle stefan@stefans-MacBook-Pro curs7 % ghci ghci GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help Prelude> :m + Control.Exception :m + Control.Exception Prelude Control.Exception> :t catch :t catch catch :: Exception e => IO a -> (e -> IO a) -> IO a Prelude Control.Exception> :i IOException :i IOException type IOException :: * data IOException = GHC.IO.Exception.IOError {GHC.IO.Exception.ioe_handle :: Maybe GHC.IO.Handle.Types.Handle, GHC.IO.Exception.ioe_type :: GHC.IO.Exception.IOErrorType, GHC.IO.Exception.ioe_location :: String, GHC.IO.Exception.ioe_description :: String, GHC.IO.Exception.ioe_errno :: Maybe Foreign.C.Types.CInt, GHC.IO.Exception.ioe_filename :: Maybe FilePath} -- Defined in ‘GHC.IO.Exception’ instance Eq IOException -- Defined in ‘GHC.IO.Exception’ instance Show IOException -- Defined in ‘GHC.IO.Exception’ instance Exception IOException -- Defined in ‘GHC.IO.Exception’ Prelude Control.Exception> :i Exception :i Exception type Exception :: * -> Constraint class (base-4.14.3.0:Data.Typeable.Internal.Typeable e, Show e) => Exception e where toException :: e -> SomeException fromException :: SomeException -> Maybe e displayException :: e -> String -- Defined in ‘GHC.Exception.Type’ instance Exception SomeAsyncException -- Defined in ‘GHC.IO.Exception’ instance Exception IOException -- Defined in ‘GHC.IO.Exception’ instance Exception Deadlock -- Defined in ‘GHC.IO.Exception’ instance Exception CompactionFailed -- Defined in ‘GHC.IO.Exception’ instance Exception BlockedIndefinitelyOnSTM -- Defined in ‘GHC.IO.Exception’ instance Exception BlockedIndefinitelyOnMVar -- Defined in ‘GHC.IO.Exception’ instance Exception AsyncException -- Defined in ‘GHC.IO.Exception’ instance Exception AssertionFailed -- Defined in ‘GHC.IO.Exception’ instance Exception ArrayException -- Defined in ‘GHC.IO.Exception’ instance Exception AllocationLimitExceeded -- Defined in ‘GHC.IO.Exception’ instance Exception ErrorCall -- Defined in ‘GHC.Exception’ instance Exception TypeError -- Defined in ‘Control.Exception.Base’ instance Exception RecUpdError -- Defined in ‘Control.Exception.Base’ instance Exception RecSelError -- Defined in ‘Control.Exception.Base’ instance Exception RecConError -- Defined in ‘Control.Exception.Base’ instance Exception PatternMatchFail -- Defined in ‘Control.Exception.Base’ instance Exception NonTermination -- Defined in ‘Control.Exception.Base’ instance Exception NoMethodError -- Defined in ‘Control.Exception.Base’ instance Exception NestedAtomically -- Defined in ‘Control.Exception.Base’ instance Exception SomeException -- Defined in ‘GHC.Exception.Type’ instance Exception ArithException -- Defined in ‘GHC.Exception.Type’ Prelude Control.Exception> :q :q Leaving GHCi. stefan@stefans-MacBook-Pro curs7 % ./curs7 ./curs7 curs7: user error (Pattern match failure in do expression at curs7.hs:192:11-22) stefan@stefans-MacBook-Pro curs7 % ./curs7 asdsahfadfa ./curs7 asdsahfadfa curs7: asdsahfadfa: openFile: does not exist (No such file or directory) stefan@stefans-MacBook-Pro curs7 % ghc curs7.hs ghc curs7.hs [1 of 1] Compiling Main ( curs7.hs, curs7.o ) curs7.hs:191:13: error: Not in scope: type constructor or class ‘IOException’ | 191 | trateaza :: IOException -> IO () | ^^^^^^^^^^^ stefan@stefans-MacBook-Pro curs7 % :r :r zsh: command not found: :r stefan@stefans-MacBook-Pro curs7 % ghc curs7.hs ghc curs7.hs [1 of 1] Compiling Main ( curs7.hs, curs7.o ) curs7.hs:193:14: error: • Couldn't match expected type ‘IO ()’ with actual type ‘[Char]’ • In the expression: "Ooops!" In an equation for ‘trateaza’: trateaza e = "Ooops!" | 193 | trateaza e = "Ooops!" | ^^^^^^^^ stefan@stefans-MacBook-Pro curs7 % ghc curs7.hs ghc curs7.hs [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 test.hs ./curs7 test.hs import System.Environment import System.IO main :: IO () main = putStrLn "Hello!" stefan@stefans-MacBook-Pro curs7 % ./curs7 fdasfjkdasfjasd ./curs7 fdasfjkdasfjasd curs7: fdasfjkdasfjasd: openFile: does not exist (No such file or directory) stefan@stefans-MacBook-Pro curs7 % ghc curs7.hs ghc curs7.hs [1 of 1] Compiling Main ( curs7.hs, curs7.o ) curs7.hs:197:19: error: parse error on input ‘<-’ Perhaps this statement should be within a 'do' block? | 197 | (handle <- openFile filename ReadMode | ^^ stefan@stefans-MacBook-Pro curs7 % ghc curs7.hs ghc curs7.hs [1 of 1] Compiling Main ( curs7.hs, curs7.o ) Linking curs7 ... stefan@stefans-MacBook-Pro curs7 % ./curs7 test.hs ./curs7 test.hs import System.Environment import System.IO main :: IO () main = putStrLn "Hello!" stefan@stefans-MacBook-Pro curs7 % ./curs7 adsfdasf ./curs7 adsfdasf Ooops! stefan@stefans-MacBook-Pro curs7 % ghci ghci GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help Prelude> :m + Control.Exception :m + Control.Exception Prelude Control.Exception> :i ArithException :i ArithException type ArithException :: * data ArithException = Overflow | Underflow | LossOfPrecision | DivideByZero | Denormal | RatioZeroDenominator -- Defined in ‘GHC.Exception.Type’ instance Eq ArithException -- Defined in ‘GHC.Exception.Type’ instance Ord ArithException -- Defined in ‘GHC.Exception.Type’ instance Show ArithException -- Defined in ‘GHC.Exception.Type’ instance Exception ArithException -- Defined in ‘GHC.Exception.Type’ Prelude Control.Exception> div 10 0 div 10 0 *** Exception: divide by zero Prelude Control.Exception> main main :4:1: error: • Variable not in scope: main • Perhaps you meant ‘min’ (imported from Prelude) Prelude Control.Exception> :l curs7 :l curs7 [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main Control.Exception> main main *** Exception: divide by zero *Main Control.Exception> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main Control.Exception> main main A aparut o problema!!! *Main Control.Exception> :m + System.Random :m + System.Random : error: Could not find module ‘System.Random’ It is not a module in the current program, or in any known package. *Main Control.Exception> :q :q Leaving GHCi. stefan@stefans-MacBook-Pro curs7 % ghc curs7 ghc curs7 [1 of 1] Compiling Main ( curs7.hs, curs7.o ) curs7.hs:4:1: error: Could not find module ‘System.Random’ Use -v (or `:set -v` in ghci) to see a list of the files searched for. | 4 | import System.Random | ^^^^^^^^^^^^^^^^^^^^ stefan@stefans-MacBook-Pro curs7 % brew search haskell brew search haskell ==> Formulae haskell-language-server stylish-haskell hspell haskell-stack ✔ taskell aspell stefan@stefans-MacBook-Pro curs7 % brew search ghc brew search ghc ==> Formulae ghc ✔ ghc@8.8 ghcup ghi ghr grc shc ghc@8.6 ghc@9 gh ghz ghq gcc If you meant "ghc" specifically: It was migrated from homebrew/cask to homebrew/core. stefan@stefans-MacBook-Pro curs7 % stack install random stack install random stefan@stefans-MacBook-Pro curs7 % ghc ghc ghc: no input files Usage: For basic information, try the `--help' option. stefan@stefans-MacBook-Pro curs7 % ghci ghci GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help Prelude> :m + System.Random :m + System.Random : error: Could not find module ‘System.Random’ It is not a module in the current program, or in any known package. Prelude> :q :q Leaving GHCi. stefan@stefans-MacBook-Pro curs7 % stack stack stack - The Haskell Tool Stack Usage: stack [--help] [--version] [--numeric-version] [--hpack-numeric-version] [--docker*] [--nix*] [--verbosity VERBOSITY | (-v|--verbose) | --silent] [--[no-]time-in-log] [--stack-root STACK-ROOT] [--work-dir WORK-DIR] [--[no-]system-ghc] [--[no-]install-ghc] [--arch ARCH] [--ghc-variant VARIANT] [--ghc-build BUILD] [-j|--jobs JOBS] [--extra-include-dirs DIR] [--extra-lib-dirs DIR] [--custom-preprocessor-extensions EXT] [--with-gcc PATH-TO-GCC] [--with-hpack HPACK] [--[no-]skip-ghc-check] [--[no-]skip-msys] [--local-bin-path DIR] [--setup-info-yaml URL] [--[no-]modify-code-page] [--[no-]allow-different-user] [--[no-]dump-logs] [--color|--colour WHEN] [--snapshot-location-base URL] [--resolver RESOLVER] [--compiler COMPILER] [--[no-]terminal] [--stack-colors|--stack-colours STYLES] [--terminal-width INT] [--stack-yaml STACK-YAML] [--lock-file ARG] COMMAND|FILE Available options: --help Show this help text --version Show version --numeric-version Show only version number --hpack-numeric-version Show only hpack's version number --docker* Run 'stack --docker-help' for details --nix* Run 'stack --nix-help' for details --verbosity VERBOSITY Verbosity: silent, error, warn, info, debug -v,--verbose Enable verbose mode: verbosity level "debug" --silent Enable silent mode: verbosity level "silent" --[no-]time-in-log Enable/disable inclusion of timings in logs, for the purposes of using diff with logs (default: enabled) --stack-root STACK-ROOT Absolute path to the global stack root directory (Overrides any STACK_ROOT environment variable) --work-dir WORK-DIR Relative path of work directory (Overrides any STACK_WORK environment variable, default is '.stack-work') --[no-]system-ghc Enable/disable using the system installed GHC (on the PATH) if it is available and its version matches. Disabled by default. --[no-]install-ghc Enable/disable downloading and installing GHC if necessary (can be done manually with stack setup) (default: enabled) --arch ARCH System architecture, e.g. i386, x86_64 --ghc-variant VARIANT Specialized GHC variant, e.g. integersimple (incompatible with --system-ghc) --ghc-build BUILD Specialized GHC build, e.g. 'gmp4' or 'standard' (usually auto-detected) -j,--jobs JOBS Number of concurrent jobs to run --extra-include-dirs DIR Extra directories to check for C header files --extra-lib-dirs DIR Extra directories to check for libraries --custom-preprocessor-extensions EXT Extensions used for custom preprocessors --with-gcc PATH-TO-GCC Use gcc found at PATH-TO-GCC --with-hpack HPACK Use HPACK executable (overrides bundled Hpack) --[no-]skip-ghc-check Enable/disable skipping the GHC version and architecture check (default: disabled) --[no-]skip-msys Enable/disable skipping the local MSYS installation (Windows only) (default: disabled) --local-bin-path DIR Install binaries to DIR --setup-info-yaml URL Alternate URL or relative / absolute path for stack dependencies --[no-]modify-code-page Enable/disable setting the codepage to support UTF-8 (Windows only) (default: enabled) --[no-]allow-different-user Enable/disable permission for users other than the owner of the stack root directory to use a stack installation (POSIX only) (default: true inside Docker, otherwise false) --[no-]dump-logs Enable/disable dump the build output logs for local packages to the console (default: dump warning logs) --color,--colour WHEN Specify when to use color in output; WHEN is 'always', 'never', or 'auto'. On Windows versions before Windows 10, for terminals that do not support color codes, the default is 'never'; color may work on terminals that support color codes --snapshot-location-base URL The base location of LTS/Nightly snapshots --resolver RESOLVER Override resolver in project file --compiler COMPILER Use the specified compiler --[no-]terminal Enable/disable overriding terminal detection in the case of running in a false terminal --stack-colors,--stack-colours STYLES Specify stack's output styles; STYLES is a colon-delimited sequence of key=value, where 'key' is a style name and 'value' is a semicolon-delimited list of 'ANSI' SGR (Select Graphic Rendition) control codes (in decimal). Use 'stack ls stack-colors --basic' to see the current sequence. In shells where a semicolon is a command separator, enclose STYLES in quotes. --terminal-width INT Specify the width of the terminal, used for pretty-print messages --stack-yaml STACK-YAML Override project stack.yaml file (overrides any STACK_YAML environment variable) --lock-file ARG Specify how to interact with lock files. Default: read/write. If resolver is overridden: read-only Available commands: build Build the package(s) in this directory/configuration install Shortcut for 'build --copy-bins' uninstall DEPRECATED: This command performs no actions, and is present for documentation only test Shortcut for 'build --test' bench Shortcut for 'build --bench' haddock Shortcut for 'build --haddock' new Create a new project from a template. Run `stack templates' to see available templates. Note: you can also specify a local file or a remote URL as a template. templates Show how to find templates available for `stack new'. `stack new' can accept a template from a remote repository (default: github), local file or remote URL. Note: this downloads the help file. init Create stack project config from cabal or hpack package specifications setup Get the appropriate GHC for your project path Print out handy path information ls List command. (Supports snapshots, dependencies and stack's styles) unpack Unpack one or more packages locally update Update the package index upgrade Upgrade to the latest stack upload Upload a package to Hackage sdist Create source distribution tarballs dot Visualize your project's dependency graph using Graphviz dot ghc Run ghc hoogle Run hoogle, the Haskell API search engine. Use the '-- ARGUMENT(S)' syntax to pass Hoogle arguments, e.g. stack hoogle -- --count=20, or stack hoogle -- server --local. exec Execute a command. If the command is absent, the first of any arguments is taken as the command. run Build and run an executable. Defaults to the first available executable if none is provided as the first argument. ghci Run ghci in the context of package(s) (experimental) repl Run ghci in the context of package(s) (experimental) (alias for 'ghci') runghc Run runghc runhaskell Run runghc (alias for 'runghc') script Run a Stack Script eval Evaluate some haskell code inline. Shortcut for 'stack exec ghc -- -e CODE' clean Delete build artefacts for the project packages. purge Delete the project stack working directories (.stack-work by default). Shortcut for 'stack clean --full' query Query general build information (experimental) list List package id's in snapshot (experimental) ide IDE-specific commands docker Subcommands specific to Docker use config Subcommands for accessing and modifying configuration values hpc Subcommands specific to Haskell Program Coverage stack's documentation is available at https://docs.haskellstack.org/ stefan@stefans-MacBook-Pro curs7 % stack ghc stack ghc ghc: no input files Usage: For basic information, try the `--help' option. stefan@stefans-MacBook-Pro curs7 % stack ghci stack ghci Note: No local targets specified, so a plain ghci will be started with no package hiding or package options. You are using snapshot: lts-17.8 If you want to use package hiding and options, then you can try one of the following: * If you want to start a different project configuration than /Users/stefan/.stack/global-project/stack.yaml, then you can use stack init to create a new stack.yaml for the packages in the current directory. * If you want to use the project configuration at /Users/stefan/.stack/global-project/stack.yaml, then you can add to its 'packages' field. Configuring GHCi with the following packages: GHCi, version 8.10.4: https://www.haskell.org/ghc/ :? for help Loaded GHCi configuration from /private/var/folders/fz/fc0rvjms0vdgllq7mwph1tfr0000gn/T/haskell-stack-ghci/2a3bbd58/ghci-script Prelude> :m + System.Random :m + System.Random Prelude System.Random> :t mkStdGen :t mkStdGen mkStdGen :: Int -> StdGen Prelude System.Random> :t random :t random random :: (Random a, RandomGen g) => g -> (a, g) Prelude System.Random> :r :r Ok, no modules loaded. Prelude System.Random> f f :5:1: error: Variable not in scope: f Prelude System.Random> :l curs7 :l curs7 [1 of 1] Compiling Main ( curs7.hs, interpreted ) curs7.hs:234:10: error: Illegal type signature: ‘Int’ Type signatures are only allowed in patterns with ScopedTypeVariables | 234 | let (n1 :: Int, stare2) = random stare1 in | ^^^^^^^^^ curs7.hs:235:10: error: Illegal type signature: ‘Int’ Type signatures are only allowed in patterns with ScopedTypeVariables | 235 | let (n2 :: Int, stare3) = random stare2 in | ^^^^^^^^^ Failed, no modules loaded. Prelude System.Random> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main System.Random> f f (-3679006524402495708,8954171660910522343) *Main System.Random> f f (-3679006524402495708,8954171660910522343) *Main System.Random> f f (-3679006524402495708,8954171660910522343) *Main System.Random> f f (-3679006524402495708,8954171660910522343) *Main System.Random> f f (-3679006524402495708,8954171660910522343) *Main System.Random> f f (-3679006524402495708,8954171660910522343) *Main System.Random> :r :r [1 of 1] Compiling Main ( curs7.hs, interpreted ) Ok, one module loaded. *Main System.Random> f f (-7093293428816966640,-8738720417580527247) *Main System.Random>