Old-style Haskell manual – Haskell – Aelve Guidehttps://guide.aelve.com/haskell/feed/category/nntfj2ys2017-05-17T20:46:56Zlxypd43cA small game: Mastermind2017-05-17T20:46:56Z<h1> <span class="item-name">A small game: Mastermind</span>
</h1>oe3ccxsfLists2017-05-17T20:45:09Z<h1> <span class="item-name">Lists</span>
</h1>xw6b5gs3A small game: Guess-the-number2017-05-17T20:45:05Z<h1> <span class="item-name">A small game: Guess-the-number</span>
</h1>fh6kav0hLoops2017-05-17T20:44:48Z<h1> <span class="item-name">Loops</span>
</h1>jojork8rFunctions2017-05-17T20:44:02Z<h1> <span class="item-name">Functions</span>
</h1>aercvxluCalculations2017-05-17T18:36:47Z<h1> <span class="item-name">Calculations</span>
</h1><p>Here's a program that calculates sum of all numbers between one number and another:</p>
<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode">main <span class="fu">=</span> <span class="kw">do</span>
a <span class="ot"><-</span> readLn <span class="co">-- (1)</span>
b <span class="ot"><-</span> readLn
putStrLn (<span class="st">"There are "</span> <span class="fu">++</span> show (b<span class="fu">-</span>a<span class="fu">+</span><span class="dv">1</span>) <span class="fu">++</span> <span class="st">" numbers"</span>) <span class="co">-- (2)</span>
putStrLn (<span class="st">"Their sum is "</span> <span class="fu">++</span> show (sum [a<span class="fu">..</span>b])) <span class="co">-- (3)</span></code></pre></div>
<p><code>a <- readLn</code> takes the entered input and turns it into something before putting it into <code>a</code> – in our case, a number. The <code>readLn</code> <em>action</em> is equivalent to this:</p>
<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode">readLn <span class="fu">=</span> read <span class="fu"><$></span> getLine</code></pre></div>
<p><code>read</code> is a function that can convert a string to a number (or something else, as needed).</p>
<p>In lines <code>(2)</code> and <code>(3)</code> we use the opposite of <code>read</code> – i.e. <code>show</code> – to turn the result of a calculation into a string. This is needed because <code>++</code> can't add a string to a number, or a number to a string – it needs two strings.</p>
<p>Finally, <code>sum [a..b]</code> lists all numbers between <code>a</code> and <code>b</code> and calculates their sum. If you wrote <code>show [a..b]</code> instead of <code>show (sum [a..b])</code>, the numbers themselves would've been printed. Try it.</p>
<hr />
<p><strong>Note 1:</strong> you may be curious what <code><$></code> is. It's an <em>operator</em> that says “do something with the result”. For instance, we could write <code>length <$> getLine</code> instead of <code>read <$> getLine</code> to make an action that gets entered text and gives you a number saying how many characters are in that text.</p>
<p><strong>Note 2:</strong> there are several operators for doing something with the result, and they have to be used in different cases. If you want to do something with the result of a simple computation (like <code>2+3</code>), don't use any operator. If you have an action (like <code>getLine</code>), you should use <code><$></code>. However, if you have an action and you want to perform another action on its result (like printing it), use <code>=<<</code>.</p>
<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode"> | action | value |
------------+----------------------+----------------+
action | putStrLn =<< getLine | print [1..3] |
computation | read <$> getLine | length [1..3] |</code></pre></div>
<p>Later we'll give precise rules about how to find out what operator should be used in what case.</p>
mqgij44zReading and writing text2017-05-17T18:22:43Z<h1> <span class="item-name">Reading and writing text</span>
</h1><p>Here's a program that waits for a name to be entered and then prints it:</p>
<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode">main <span class="fu">=</span> <span class="kw">do</span>
name <span class="ot"><-</span> getLine <span class="co">-- (1)</span>
putStrLn (<span class="st">"Hello, "</span> <span class="fu">++</span> name) <span class="co">-- (2)</span></code></pre></div>
<p><code>name <- getLine</code> takes entered text and calls it <code>name</code> so that in the rest of the program we'd be able to use it. In the line <code>(2)</code> we add “Hello” to <code>name</code> and show the result on screen.</p>
<hr />
<p><strong>Note 1:</strong> of course, the original <code>name</code> doesn't change when we do <code>putStrLn ("Hello, " ++ name)</code>. If it did, that'd be pretty inconvenient.</p>
<p><strong>Note 2:</strong> parentheses around <code>("Hello, " ++ name)</code> are important. Without them, <code>putStrLn</code> would try to print <code>"Hello, "</code> instead of <code>"Hello, " ++ name</code>, and then <code>++ name</code> would confuse the compiler and you'll see this error:</p>
<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode">foo.hs:3:3: error:
• Couldn't match expected type ‘[Char]’ with actual type ‘IO ()’
• In the first argument of ‘(++)’, namely ‘putStrLn "Hello, "’
In a stmt of a 'do' block: putStrLn "Hello, " ++ name
In the expression:
do { name <- getLine;
putStrLn "Hello, " ++ name }</code></pre></div>
r0mluzmrWriting and running programs2017-05-17T18:15:49Z<h1> <span class="item-name">Writing and running programs</span>
</h1><p>The command <code>stack runghc foo.hs</code> runs a program contained in <code>foo.hs</code>. For instance, this program prints “Hello world” to the screen:</p>
<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode"><span class="co">-- This should be put into a file called 'foo.hs'.</span>
<span class="co">--</span>
<span class="co">-- Lines starting with “--” are comments and they</span>
<span class="co">-- are ignored by Haskell.</span>
main <span class="fu">=</span> <span class="kw">do</span>
putStrLn <span class="st">"Hello world"</span></code></pre></div>
<p><code>stack</code> can be obtained here: <a href="https://docs.haskellstack.org/en/stable/README/#how-to-install">https://docs.haskellstack.org/en/stable/README/#how-to-install</a>. Before running programs, you would need to install a <em>compiler</em>; to do that, run <code>stack setup</code>.</p>
<hr />
<p><strong>Note 1:</strong> lines starting with <code>--</code> are <em>comments.</em> They can be left in code or deleted, as you wish. You may want to leave comments in your programs even if you understand everything at the moment of writing, because later on you might forget and become unable to read your own code.</p>