Safe HaskellSafe-Inferred

Lessons.Lesson06

Description

Notes taken by Emilija Rimšelytė

Synopsis

Documentation

main :: IO () Source #

Entry point of a Haskell program. putStrLn performs an effect: it prints to the terminal. In Haskell, effects live in IO — they’re not just values, they’re actions.

main' :: IO String Source #

Reads a line from the terminal. This is an IO action — it doesn’t give you a String directly, it gives you a computation that will produce a String when run.

m :: IO () Source #

Reads and prints a line. Demonstrates how to extract a value from IO using '<-' inside a 'do' block. You can't escape IO in pure code — but inside 'do', you can unwrap and use.

queryName :: IO String Source #

Asks the user for their name. This is interactive, so it lives in IO. Side effects like printing and reading are sequenced here.

aGame :: IO () Source #

Combines IO (input/output) with pure logic. The greeting is pure, but getting the name is not.

pureF :: String -> IO String Source #

This won’t compile, as you can see, IO and String can’t mix so free. pureF :: String -> String pureF a = queryName ++ a

Mixes IO with pure string manipulation. You extract the name, then glue it tight, But wrap it in IO to make it right.

pureF' :: IO String Source #

Returns a constant string wrapped in IO.

action :: IO () Source #

Waits 10 seconds, then prints Hi. Demonstrates time delay — an effect, so it’s in IO.

threading :: IO () Source #

Starts two concurrent threads using forkIO. Each runs action independently. Threads in Haskell are lightweight — managed by the runtime, not the OS.

actionC :: Chan String -> IO () Source #

Channels let threads communicate safely. Writes to the channel, after a pause, Communication between threads — that’s the cause.

threading' :: IO () Source #

Demonstrates inter-thread communication. Two threads write to the same channel. Main thread reads both messages and prints them.

actionA :: String -> IO String Source #

Delayed computation that returns a string. Used with async to run in parallel.

threading'' :: IO String Source #

Runs two async actions concurrently. Waits for both to finish, then combines results.

transfer :: TVar Integer -> TVar Integer -> Integer -> STM () Source #

Transfers money between two accounts using STM. STM = Software Transactional Memory. It’s atomic, composable, and avoids locks.

runTx :: IO () Source #

Demonstrates STM in action. One thread tries to transfer too much, Another adds funds — STM retries until it succeeds.