<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title xmlns:ns="http://www.w3.org/2005/Atom" ns:type="text">Do notation – Haskell – Aelve Guide</title><id>https://guide.aelve.com/haskell/feed/category/eg3jkvay</id><updated>2018-10-30T17:06:36Z</updated><link xmlns:ns="http://www.w3.org/2005/Atom" ns:href="https://guide.aelve.com/haskell/feed/category/eg3jkvay"/><entry><id>vgy90kyo</id><title xmlns:ns="http://www.w3.org/2005/Atom" ns:type="text">When NOT to use do</title><updated>2018-10-30T17:06:36Z</updated><content xmlns:ns="http://www.w3.org/2005/Atom" ns:type="html">&lt;h1&gt;  &lt;span class=&#34;item-name&#34;&gt;When NOT to use do&lt;/span&gt;

&lt;/h1&gt;&lt;p&gt;There are a number of situations where using &lt;code&gt;do&lt;/code&gt; notation doesn&#39;t buy you anything and only results in less concise code. The sections below contain pairs of code blocks that are equivalent where the latter should be preferred.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Single statement &lt;code&gt;do&lt;/code&gt; blocks&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A &lt;code&gt;do&lt;/code&gt; block with a single statement &lt;code&gt;s&lt;/code&gt; is equivalent to &lt;code&gt;s&lt;/code&gt; on its own.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34;&gt;&lt;pre class=&#34;sourceCode&#34;&gt;&lt;code class=&#34;sourceCode&#34;&gt;oneLineDo name &lt;span class=&#34;fu&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;do&lt;/span&gt;
  putStrLn (&lt;span class=&#34;st&#34;&gt;&amp;quot;Hi there &amp;quot;&lt;/span&gt; &lt;span class=&#34;fu&#34;&gt;++&lt;/span&gt; name)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&#34;sourceCode&#34;&gt;&lt;pre class=&#34;sourceCode&#34;&gt;&lt;code class=&#34;sourceCode&#34;&gt;oneLineDoSimplified name &lt;span class=&#34;fu&#34;&gt;=&lt;/span&gt; putStrLn (&lt;span class=&#34;st&#34;&gt;&amp;quot;Hi there &amp;quot;&lt;/span&gt; &lt;span class=&#34;fu&#34;&gt;++&lt;/span&gt; name)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Assignment then return&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the second to last line in a &lt;code&gt;do&lt;/code&gt; block is a variable binding (&lt;code&gt;x &amp;lt;- bar&lt;/code&gt;) and the last line is &lt;code&gt;return x&lt;/code&gt;, they can be replaced by simply &lt;code&gt;bar&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34;&gt;&lt;pre class=&#34;sourceCode&#34;&gt;&lt;code class=&#34;sourceCode&#34;&gt;assignAndReturn &lt;span class=&#34;fu&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;do&lt;/span&gt;
  x &lt;span class=&#34;ot&#34;&gt;&amp;lt;-&lt;/span&gt; bar
  return x&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&#34;sourceCode&#34;&gt;&lt;pre class=&#34;sourceCode&#34;&gt;&lt;code class=&#34;sourceCode&#34;&gt;assignAndReturnSimplified &lt;span class=&#34;fu&#34;&gt;=&lt;/span&gt; bar&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Only bind needed&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the &lt;code&gt;do&lt;/code&gt; block consists of a sequence of variable binding via &lt;code&gt;x &amp;lt;-&lt;/code&gt; followed by passing &lt;code&gt;x&lt;/code&gt; to another function, these can be replaced by monadic function composition using the &lt;em&gt;bind&lt;/em&gt; operator &lt;code&gt;&amp;gt;&amp;gt;=&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34;&gt;&lt;pre class=&#34;sourceCode&#34;&gt;&lt;code class=&#34;sourceCode&#34;&gt;onlyBindNeeded &lt;span class=&#34;fu&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;do&lt;/span&gt;
  x &lt;span class=&#34;ot&#34;&gt;&amp;lt;-&lt;/span&gt; getX
  y &lt;span class=&#34;ot&#34;&gt;&amp;lt;-&lt;/span&gt; getY x
  return y&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&#34;sourceCode&#34;&gt;&lt;pre class=&#34;sourceCode&#34;&gt;&lt;code class=&#34;sourceCode&#34;&gt;onlyBindNeededSimplified &lt;span class=&#34;fu&#34;&gt;=&lt;/span&gt; getX &lt;span class=&#34;fu&#34;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; getY&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Only lift needed:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the &lt;code&gt;do&lt;/code&gt; block consists of a number of variable binding via &lt;code&gt;&amp;lt;-&lt;/code&gt; and the last line &lt;code&gt;return&lt;/code&gt;s a value constructed from the bindings, prefer using the &lt;code&gt;liftA&amp;lt;n&amp;gt;&lt;/code&gt; functions (if your &lt;em&gt;&amp;lt;n&amp;gt;&lt;/em&gt; is small) or using applicative style via &lt;code&gt;&amp;lt;$&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;*&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;sourceCode&#34;&gt;&lt;pre class=&#34;sourceCode&#34;&gt;&lt;code class=&#34;sourceCode&#34;&gt;&lt;span class=&#34;ot&#34;&gt;onlyLiftNeeded ::&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;IO&lt;/span&gt; (a, b)
onlyLiftNeeded &lt;span class=&#34;fu&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kw&#34;&gt;do&lt;/span&gt;
  a &lt;span class=&#34;ot&#34;&gt;&amp;lt;-&lt;/span&gt; getA
  b &lt;span class=&#34;ot&#34;&gt;&amp;lt;-&lt;/span&gt; getB
  return (a, b)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&#34;sourceCode&#34;&gt;&lt;pre class=&#34;sourceCode&#34;&gt;&lt;code class=&#34;sourceCode&#34;&gt;&lt;span class=&#34;ot&#34;&gt;onlyLiftNeededLiftA2 ::&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;IO&lt;/span&gt; (a, b)
onlyLiftNeededLiftA2 &lt;span class=&#34;fu&#34;&gt;=&lt;/span&gt; liftA2 (,) getA getB&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&#34;sourceCode&#34;&gt;&lt;pre class=&#34;sourceCode&#34;&gt;&lt;code class=&#34;sourceCode&#34;&gt;&lt;span class=&#34;ot&#34;&gt;onlyLiftNeededApplicativeStyle ::&lt;/span&gt; &lt;span class=&#34;dt&#34;&gt;IO&lt;/span&gt; (a, b)
onlyLiftNeededApplicativeStyle &lt;span class=&#34;fu&#34;&gt;=&lt;/span&gt; (,) &lt;span class=&#34;fu&#34;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; getA &lt;span class=&#34;fu&#34;&gt;&amp;lt;*&amp;gt;&lt;/span&gt; getB&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
</content><link xmlns:ns="http://www.w3.org/2005/Atom" ns:href="https://guide.aelve.com/haskell/do-notation-eg3jkvay#item-vgy90kyo"/></entry></feed>