Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is my variant</p> <pre><code>import Data.List decimal :: [Int] -&gt; Int decimal xs = foldl' (\sum (pos,x) -&gt; (sum + x*(10^(l-pos)))) 0 $ zip [1..] xs where l = length xs *Main&gt; decimal [1,4,3,1,9] 14319 </code></pre> <p>In <code>Haskell</code>, you have really powerfull weapon - functions for lists processing. One of these functions is <code>foldl</code> (we use strict version of <code>foldl</code>, <code>foldl'</code>) It's type</p> <pre><code>foldl :: (a -&gt; b -&gt; a) -&gt; a -&gt; [b] -&gt; a </code></pre> <p>This functions takes thre arguments, an accumulating agrument, a list processed, and, the most interest, the function that perform any operation with accumulator and list element and returns the result. Fold is really significant function so you should read detail <a href="http://www.haskell.org/haskellwiki/Fold" rel="nofollow">manual</a> about it.<br> But, there is a problem, we have three variables it our equation: list element processed (<code>x</code>), total list length (<code>n</code>) and position of processed element (<code>k</code>). But we can traverse to <code>foldl</code> only one element. How can we traverse position of each element? Let's form tuples from <code>Int</code> where first element is a position, and second is a value. It is a standard trick, <code>zip</code> function helps us:</p> <pre><code>zip [1..] [1,4,3,4,6] [(1,1),(2,4),(3,3),(4,4),(5,6)] </code></pre> <p>Than we pass our list of tuples into <code>foldl</code> function, and <code>foldl</code> call <code>lambda function</code> <code>(\sum (pos,x) -&gt; (sum + x*(10^(l-pos))))</code> for each element of list, summing result in <code>sum</code></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. 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