Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, as far as <code>liftM</code> functions go, my preferred way to use those is the combinators defined in <code>Control.Applicative</code>. Using those, you'd be able to write <code>last = mod &lt;$&gt; ps &lt;*&gt; [10]</code>. The <code>ap</code> function from <code>Control.Monad</code> does the same thing, but I prefer the infix version.</p> <p>What <code>(&lt;$&gt;)</code> and <code>(&lt;*&gt;)</code> goes like this: <code>liftM2</code> turns a function <code>a -&gt; b -&gt; c</code> into a function <code>m a -&gt; m b -&gt; m c</code>. Plain <code>liftM</code> is just <code>(a -&gt; b) -&gt; (m a -&gt; m b)</code>, which is the same as <code>fmap</code> and also <code>(&lt;$&gt;)</code>.</p> <p>What happens if you do that to a multi-argument function? It turns something like <code>a -&gt; b -&gt; c -&gt; d</code> into <code>m a -&gt; m (b -&gt; c -&gt; d)</code>. This is where <code>ap</code> or <code>(&lt;*&gt;)</code> come in: what they do is turn something like <code>m (a -&gt; b)</code> into <code>m a -&gt; m b</code>. So you can keep stringing it along that way for as many arguments as you like.</p> <hr> <p>That said, Travis Brown is correct that, in this case, it seems you don't really need any of the above. In fact, you can simplify your function a great deal: For instance, both <code>last</code> and <code>next</code> can be written as single-argument functions mapped over the same list, <code>ps</code>, and <code>zipWith</code> is the same as a <code>zip</code> and a <code>map</code>. All of these maps can be combined and pushed down into the <code>alts</code> function. This makes <code>alts</code> a single-argument function, eliminating the <code>zip</code> as well. Finally, the <code>concat</code> can be combined with the <code>map</code> as <code>concatMap</code> or, if preferred, <code>(&gt;&gt;=)</code>. Here's what it ends up:</p> <pre><code>increasing' :: Integer -&gt; [Integer] increasing' 1 = [1..9] increasing' n = increasing' (n - 1) &gt;&gt;= alts where alts x = map ((x * 10) +) [mod x 10..9] </code></pre> <p>Note that all refactoring I did to get to that version from yours was <em>purely syntactic</em>, only applying transformations that should have no impact on the result of the function. Equational reasoning and referential transparency are nice!</p>
    singulars
    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. 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.
    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