Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I guess you are doing this in the <code>ghci</code> interpreter. Then, have a look at (<code>:t</code> displays the type of an expression and a line of the form <code>a :: t</code> means <code>a</code> has type <code>t</code>):</p> <pre><code>Prelude&gt; let a = 3 Prelude&gt; :t a a :: Integer </code></pre> <p>The <code>ghci</code> interpreter commits early and gives <code>a</code> the type <code>Integer</code> though it should give any numeric type (thus <code>a :: Num t =&gt; t</code>).</p> <p>Now, your function receives <code>Int</code>s as arguments but since <code>a</code> and <code>b</code> are <code>Integer</code>s you get that error message.</p> <p>You can either remove the restrictive type signature or you can define <code>a</code> and <code>b</code> to be <code>Int</code>s. I'd go with the first option, unless there is some requirement to go with <code>Int</code>-only type signature. To do so you need to add <code>::Int</code> at the end of the definition:</p> <pre><code>Prelude&gt; let b = 42 :: Int Prelude&gt; :t b b :: Int </code></pre> <p>If you want to remove the signature recode your function to have only one line:</p> <pre><code>max' a b = if a &gt;= b then a else b </code></pre> <p>Now, if you're to inspect its type:</p> <pre><code>Prelude&gt; :t max' max' :: Ord a =&gt; a -&gt; a -&gt; a </code></pre> <p>Which means you've got a generic function which works for any type which can be ordered.</p> <p><strong>An alternative</strong> is to start <code>ghci</code> using an extension: <code>ghci -XNoMonomorphismRestriction</code>. In this case:</p> <pre><code>Prelude&gt; let a = 3 Prelude&gt; :t a a :: Num a =&gt; a </code></pre> <p>which will work directly on your function.</p> <p>The reason why <code>ghci</code> without this extension commits to <code>Integer</code> is the <a href="http://www.haskell.org/haskellwiki/Monomorphism_restriction" rel="noreferrer">Monomorphism restriction</a></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