Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, so let's break this down.</p> <p>If you're only interested in a normal list of ints, you've got</p> <pre><code> findPosition :: [Int] -&gt; [Int] </code></pre> <p>How can you implement that? Well, uh, you need an input for the thing you're actually searching for!</p> <pre><code> findPosition :: Int -&gt; [Int] -&gt; [Int] </code></pre> <p>OK, cool. So the built-in <code>elem</code> function tells you <em>if</em> the element you want is there. But we want it's <em>position</em>. So how? Well, you can "label" every element with it's position, like so:</p> <pre><code> label :: [x] -&gt; [(Int, x)] label = zip [0..] </code></pre> <p>Now we can use <code>filter</code> to find all the items:</p> <pre><code> find :: (Eq x) =&gt; x -&gt; [(Int, x)] -&gt; [(Int, x)] find x0 = filter (\ (n, x) -&gt; x == x0) </code></pre> <p>But we only want the actual positions, not the <code>x</code>s (which are all identical at this point). So we can <code>map fst</code> to get that.</p> <p>Assembling it all,</p> <pre><code> findPosition :: Int -&gt; [Int] -&gt; [Int] findPosition x0 = map fst . filter (\ (n, x) -&gt; x == x0) . zip [0..] </code></pre> <hr> <p>That's great! But you wanted a list <em>of lists</em> of ints, right?</p> <p>I would suggest you change your requirement spec to return each "coordinate" as a tuple rather than a list. That is, make it so</p> <pre><code>findPosition 8 [[1,2,8],[4,5,6],[7,8,9]] =&gt; [(1, 3), (3, 2)] </code></pre> <p>It's probably less confusing that way. Hopefully I've given you enough hints to figure things out from here...</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. 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