Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like homework or something so I will give you enough hint so that you can work rest of the details yourself.</p> <pre><code>fmap1 :: (a -&gt; b) -&gt; IO a -&gt; IO b fmap1 f action = </code></pre> <p><code>action</code> is as <code>IO</code> action and <code>f</code> is a function from <code>a</code> to <code>b</code> and hence type <code>a -&gt; b</code>.</p> <p>If you are familiar with monadic bind <code>&gt;&gt;=</code> which has type (simplified for <code>IO</code> monad)</p> <pre><code>(&gt;&gt;=) :: IO a -&gt; (a -&gt; IO b) -&gt; IO b </code></pre> <p>Now if you look at </p> <pre><code>action &gt;&gt;= f </code></pre> <p>It means perform the <code>IO</code> action which returns an output (say <code>out</code> of type <code>a</code>) and pass the output to <code>f</code> which is of type <code>a -&gt; IO b</code> and hence <code>f out</code> is of type <code>IO b</code>.</p> <p>If you look at the second function called <code>return</code> which has type (again simlified for <code>IO</code> monad)</p> <pre><code>return :: a -&gt; IO a </code></pre> <p>It takes a pure value of type <code>a</code> and gives an <code>IO</code> action of type <code>IO a</code>.</p> <p>Now lets look back to <code>fmap</code>.</p> <pre><code>fmap1 f action </code></pre> <p>which performs the <code>IO</code> action and then runs <code>f</code> on the output of the action and then converts the output to another <code>IO</code> action of type <code>IO b</code>. Therefore</p> <pre><code>fmap1 f action = action &gt;&gt;= g where g out = return (f out) </code></pre> <p>Now comes the syntactic sugar of <code>do</code> notation. Which is just to write bind <code>&gt;&gt;=</code> in another way. </p> <p>In <code>do</code> notation you can get the output of an action by </p> <pre><code>out &lt;- action </code></pre> <p>So bind just reduces to </p> <pre><code>action &gt;&gt;= f = do out &lt;- action f out </code></pre> <p>I think now you will be able to convert the definition of fmap to do construct. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload