Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addition to Guido's solution, there is more than one way to do it in haskell.</p> <p>Please take a look at <code>fst</code> and <code>snd</code>, which takes the first / second element out of a pair, respectively.</p> <pre><code>GHCi&gt; :t fst fst :: (a, b) -&gt; a GHCi&gt; :t snd snd :: (a, b) -&gt; b </code></pre> <p>You should be familiar with <code>map</code> if you are playing with functional programming languages, which takes a function and a list, applies the function on every element of that list, and gives you all the results in another list: </p> <pre><code>GHCi&gt; :t map map :: (a -&gt; b) -&gt; [a] -&gt; [b] </code></pre> <p>Given a list of pairs, you want two lists, one contains all first elements in order, and the other contains all second elements: </p> <pre><code>GHCi&gt; let split xs = (map fst xs, map snd xs) GHCi&gt; split [(1,2),(3,4),(5,6)] ([1,3,5],[2,4,6]) GHCi&gt; </code></pre> <p>One step further, as @jozefg has pointed out in the comment, that this method is not efficient as @Guido 's one, but we can make some changes to improve it (which is exactly what @Guido 's solution):</p> <p>Now it's time to take a look at how <code>map</code> is implemented <a href="http://hackage.haskell.org/package/base-4.6.0.1/docs/src/GHC-Base.html#map" rel="nofollow">here</a></p> <pre><code>map :: (a -&gt; b) -&gt; [a] -&gt; [b] map _ [] = [] map f (x:xs) = f x : map f xs </code></pre> <p>so we can try to change our <code>split</code> a little:</p> <p>we still need the base case, (i.e. what if <code>xs</code> is empty):</p> <pre><code>split [] = ([], []) split ls = (map fst ls, map snd ls) -- attention! </code></pre> <p>and we break the list into head and tail, just like <code>map</code>:</p> <pre><code>split (x:xs) = (fst x: map fst xs, snd x: map snd xs) </code></pre> <p>Now we can do a pattern matching, <code>(a,b) = x</code>, so we don't have to call two individual functions to break a pair into two:</p> <pre><code>split (x:xs) = (a: map fst xs, b: map snd xs) where (a,b) = x </code></pre> <p>Compare the code here with the line I commented "attention!", have you realized that if we know the result of <code>(map fst xs, map snd xs)</code>, we can simply reuse that result to speed up. Luckily, we already have <code>split ls = (map fst ls, map snd ls)</code>!</p> <p>Using this fact, we finally come up with this version:</p> <pre><code>split [] = ([], []) split (x:xs) = (a:as , b:bs) where (a,b) = x (as,bs) = split xs </code></pre> <p>So there are essentially the same! (but as you can see, the last version we have is more efficient.)</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.
 

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