Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are right about the <code>maximum</code> part. To get the <em>list</em> (that's what Haskell's []s are, arrays are different structures) you need to use the <code>map</code> higher-order function, like this:</p> <pre><code>chainLength n = length (chain n) lengths = map chainLength [1..1000000] </code></pre> <p>Essentially, <code>map</code> takes as arguments a function and a list. It applies the function to each element in the list and returns the list of the results.</p> <p>Since you will be needing the number whose chain has that length, you may want change the <code>chainLength</code> function to return the number as well, like this:</p> <pre><code>chainLength n = (n, length (chain n)) </code></pre> <p>That way you will have an array of pairs, with each number and its chain length.</p> <p>Now you need to get the pair with the largest second component. That's where the <code>maximumBy</code> function comes in. It works just like <code>maximum</code> but takes a function as a parameter to select how to compare the values. In this case, the second component of the pair. This comparison function takes two numbers and returns a value of type <code>Ordering</code>. This type has only three possible values: <code>LT</code>, <code>EQ</code>, <code>GT</code>, for less than, equal, and greater than, respectively.</p> <p>So, we need a function that given two pairs tells us how the second components compare to each other:</p> <pre><code>compareSnd (_, y1) (_, y2) = compare y1 y2 -- Or, if you import Data.Function, you can write it like this (thanks alexey_r): compareSnd = compare `on` snd -- reads nicely </code></pre> <p>I used the default <code>compare</code> function that compares numbers (well, <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Ord.html#v%3Acompare" rel="nofollow noreferrer">not just numbers</a>).</p> <p>Now we only need to get the maximum using this function:</p> <pre><code>longestChain = maximumBy compareSnd lengths </code></pre> <p>That gets you a pair of the number with the longest chain and the corresponding length. Feel free to apply <code>fst</code> and <code>snd</code> as you please.</p> <p>Note that this could be more much more concisely using <code>zip</code> and composition, but since you tagged the question as newbie, I thought it better to break it down like this.</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