-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLessons-Lesson06.html
More file actions
28 lines (28 loc) · 8.56 KB
/
Lessons-Lesson06.html
File metadata and controls
28 lines (28 loc) · 8.56 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>Lessons.Lesson06</title><link href="linuwial.css" rel="stylesheet" type="text/css" title="Linuwial" /><link rel="stylesheet" type="text/css" href="quick-jump.css" /><link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=PT+Sans:400,400i,700" /><script src="haddock-bundle.min.js" async="async" type="text/javascript"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({ tex2jax: { processClass: "mathjax", ignoreClass: ".*" } });</script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script></head><body><div id="package-header"><span class="caption empty"> </span><ul class="links" id="page-menu"><li><a href="src/Lessons.Lesson06.html">Source</a></li><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul></div><div id="content"><div id="module-header"><table class="info"><tr><th>Safe Haskell</th><td>Safe-Inferred</td></tr></table><p class="caption">Lessons.Lesson06</p></div><div id="description"><p class="caption">Description</p><div class="doc"><p>Notes taken by Emilija Rimšelytė</p></div></div><div id="synopsis"><details id="syn"><summary>Synopsis</summary><ul class="details-toggle" data-details-id="syn"><li class="src short"><a href="#v:main">main</a> :: IO ()</li><li class="src short"><a href="#v:main-39-">main'</a> :: IO String</li><li class="src short"><a href="#v:m">m</a> :: IO ()</li><li class="src short"><a href="#v:queryName">queryName</a> :: IO String</li><li class="src short"><a href="#v:aGame">aGame</a> :: IO ()</li><li class="src short"><a href="#v:pureF">pureF</a> :: String -> IO String</li><li class="src short"><a href="#v:pureF-39-">pureF'</a> :: IO String</li><li class="src short"><a href="#v:action">action</a> :: IO ()</li><li class="src short"><a href="#v:threading">threading</a> :: IO ()</li><li class="src short"><a href="#v:actionC">actionC</a> :: Chan String -> IO ()</li><li class="src short"><a href="#v:threading-39-">threading'</a> :: IO ()</li><li class="src short"><a href="#v:actionA">actionA</a> :: String -> IO String</li><li class="src short"><a href="#v:threading-39--39-">threading''</a> :: IO String</li><li class="src short"><a href="#v:transfer">transfer</a> :: TVar Integer -> TVar Integer -> Integer -> STM ()</li><li class="src short"><a href="#v:runTx">runTx</a> :: IO ()</li></ul></details></div><div id="interface"><h1>Documentation</h1><div class="top"><p class="src"><a id="v:main" class="def">main</a> :: IO () <a href="src/Lessons.Lesson06.html#main" class="link">Source</a> <a href="#v:main" class="selflink">#</a></p><div class="doc"><p>Entry point of a Haskell program.
<code>putStrLn</code> performs an effect: it prints to the terminal.
In Haskell, effects live in IO — they’re not just values, they’re actions.</p></div></div><div class="top"><p class="src"><a id="v:main-39-" class="def">main'</a> :: IO String <a href="src/Lessons.Lesson06.html#main%27" class="link">Source</a> <a href="#v:main-39-" class="selflink">#</a></p><div class="doc"><p>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.</p></div></div><div class="top"><p class="src"><a id="v:m" class="def">m</a> :: IO () <a href="src/Lessons.Lesson06.html#m" class="link">Source</a> <a href="#v:m" class="selflink">#</a></p><div class="doc"><p>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.</p></div></div><div class="top"><p class="src"><a id="v:queryName" class="def">queryName</a> :: IO String <a href="src/Lessons.Lesson06.html#queryName" class="link">Source</a> <a href="#v:queryName" class="selflink">#</a></p><div class="doc"><p>Asks the user for their name.
This is interactive, so it lives in IO.
Side effects like printing and reading are sequenced here.</p></div></div><div class="top"><p class="src"><a id="v:aGame" class="def">aGame</a> :: IO () <a href="src/Lessons.Lesson06.html#aGame" class="link">Source</a> <a href="#v:aGame" class="selflink">#</a></p><div class="doc"><p>Combines IO (input/output) with pure logic.
The greeting is pure, but getting the name is not.</p></div></div><div class="top"><p class="src"><a id="v:pureF" class="def">pureF</a> :: String -> IO String <a href="src/Lessons.Lesson06.html#pureF" class="link">Source</a> <a href="#v:pureF" class="selflink">#</a></p><div class="doc"><p>This won’t compile, as you can see,
IO and String can’t mix so free.
pureF :: String -> String
pureF a = queryName ++ a</p><p>Mixes IO with pure string manipulation.
You extract the name, then glue it tight,
But wrap it in IO to make it right.</p></div></div><div class="top"><p class="src"><a id="v:pureF-39-" class="def">pureF'</a> :: IO String <a href="src/Lessons.Lesson06.html#pureF%27" class="link">Source</a> <a href="#v:pureF-39-" class="selflink">#</a></p><div class="doc"><p>Returns a constant string wrapped in IO.</p></div></div><div class="top"><p class="src"><a id="v:action" class="def">action</a> :: IO () <a href="src/Lessons.Lesson06.html#action" class="link">Source</a> <a href="#v:action" class="selflink">#</a></p><div class="doc"><p>Waits 10 seconds, then prints <a href="Hi.html">Hi</a>.
Demonstrates time delay — an effect, so it’s in IO.</p></div></div><div class="top"><p class="src"><a id="v:threading" class="def">threading</a> :: IO () <a href="src/Lessons.Lesson06.html#threading" class="link">Source</a> <a href="#v:threading" class="selflink">#</a></p><div class="doc"><p>Starts two concurrent threads using <code>forkIO</code>.
Each runs <code><a href="Lessons-Lesson06.html#v:action" title="Lessons.Lesson06">action</a></code> independently.
Threads in Haskell are lightweight — managed by the runtime, not the OS.</p></div></div><div class="top"><p class="src"><a id="v:actionC" class="def">actionC</a> :: Chan String -> IO () <a href="src/Lessons.Lesson06.html#actionC" class="link">Source</a> <a href="#v:actionC" class="selflink">#</a></p><div class="doc"><p>Channels let threads communicate safely.
Writes to the channel, after a pause,
Communication between threads — that’s the cause.</p></div></div><div class="top"><p class="src"><a id="v:threading-39-" class="def">threading'</a> :: IO () <a href="src/Lessons.Lesson06.html#threading%27" class="link">Source</a> <a href="#v:threading-39-" class="selflink">#</a></p><div class="doc"><p>Demonstrates inter-thread communication.
Two threads write to the same channel.
Main thread reads both messages and prints them.</p></div></div><div class="top"><p class="src"><a id="v:actionA" class="def">actionA</a> :: String -> IO String <a href="src/Lessons.Lesson06.html#actionA" class="link">Source</a> <a href="#v:actionA" class="selflink">#</a></p><div class="doc"><p>Delayed computation that returns a string.
Used with <code>async</code> to run in parallel.</p></div></div><div class="top"><p class="src"><a id="v:threading-39--39-" class="def">threading''</a> :: IO String <a href="src/Lessons.Lesson06.html#threading%27%27" class="link">Source</a> <a href="#v:threading-39--39-" class="selflink">#</a></p><div class="doc"><p>Runs two async actions concurrently.
Waits for both to finish, then combines results.</p></div></div><div class="top"><p class="src"><a id="v:transfer" class="def">transfer</a> :: TVar Integer -> TVar Integer -> Integer -> STM () <a href="src/Lessons.Lesson06.html#transfer" class="link">Source</a> <a href="#v:transfer" class="selflink">#</a></p><div class="doc"><p>Transfers money between two accounts using STM.
STM = Software Transactional Memory.
It’s atomic, composable, and avoids locks.</p></div></div><div class="top"><p class="src"><a id="v:runTx" class="def">runTx</a> :: IO () <a href="src/Lessons.Lesson06.html#runTx" class="link">Source</a> <a href="#v:runTx" class="selflink">#</a></p><div class="doc"><p>Demonstrates STM in action.
One thread tries to transfer too much,
Another adds funds — STM retries until it succeeds.</p></div></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.29.2</p></div></body></html>