Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let <code>x</code> and <code>y</code> be the two numbers. Then one can remove the digits in <code>x</code> which it has in common with <code>y</code> like this:</p> <pre><code>Prelude&gt; import Data.List Prelude Data.List&gt; let x = 68 Prelude Data.List&gt; let y = 76 Prelude Data.List&gt; read (show x \\ show y) :: Int 8 </code></pre> <p>Flip <code>x</code> and <code>y</code> to remove digits from <code>y</code>. This uses <code>xs</code><a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3A%5C%5C" rel="nofollow noreferrer"><code>Data.List.(\\)</code></a><code>ys</code>, which removes the <em>first</em> occurrence of each element in <code>ys</code> from <code>xs</code>.</p> <hr> <p><strong>Edit:</strong> you may have solved <a href="http://projecteuler.net/index.php?section=problems&amp;id=33" rel="nofollow noreferrer">problem 33</a> by now. If not, let me give you some more hints:</p> <p>The code which I gave above, i.e. <code>read (show x \\ show y)</code> is not really suitable for this problem. What happens if <code>x</code> equals <em>ab</em> and <code>y</code> equals <em>ba</em>, for some digits <em>a</em> and <em>b</em>?</p> <p>The solution to this problem <em>can</em> be written as a single list comprehension. You may want to use <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3Aintersect" rel="nofollow noreferrer"><code>Data.List.intersect</code></a> and <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3Anub" rel="nofollow noreferrer"><code>Data.List.nub</code></a> to create the following term in your list comprehension:</p> <pre><code>s &lt;- nub $ intersect (show x) (show y) </code></pre> <p>Now <code>s</code> ranges over the digits that <code>x</code> and <code>y</code> have in common. You can remove these from <code>x</code> and <code>y</code> using</p> <pre><code>map (read . delete s . show) [x, y] </code></pre> <p>I cannot be more explicit without solving the exercise for you, but I'll leave you with two more hints:</p> <ul> <li>In your code you write <code>y &lt;- [10..99], x &lt;- [10..y], x /= y</code>. Observe that this can be written more succinctly as <code>y &lt;- [10..99], x &lt;- [10..y-1]</code>.</li> <li>Have a look at <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Ratio.html" rel="nofollow noreferrer"><code>Data.Ratio</code></a>, which provides an easy way to test the equality of rational numbers and automatically calculates the numerator and denominator of a fraction in its reduced form.</li> </ul>
 

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