Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Haskell is a bit unusual in this way. Yes you can't divide to integers together but it's rarely a problem.</p> <p>The reason is that if you look at the <code>Num</code> typeclass, there's a function <code>fromIntegral</code> this allows you to convert literals into the appropriate type. This with type inference alleviates 99% of the cases where it'd be a problem. Quick example:</p> <pre><code>newtype Foo = Foo Integer deriving (Show, Eq) instance Num Foo where fromInteger _ = Foo 0 negate = undefined abs = undefined (+) = undefined (-) = undefined (*) = undefined signum = undefined </code></pre> <p>Now if we load this into GHCi</p> <pre><code>*&gt; 0 :: Foo Foo 0 *&gt; 1 :: Foo Foo 0 </code></pre> <p>So you see we are able to do some pretty cool things with how GHCi parses a raw integer. This has a lot of practical uses in DSL's that we won't talk about here.</p> <p>Next question was how to get from a Double to an Integer or vice versa. There's a function for that.</p> <p>In the case of going from an Integer to a Double, we'd use <code>fromInteger</code> as well. Why?</p> <p>Well the type signature for it is</p> <pre><code>(Num a) =&gt; Integer -&gt; a </code></pre> <p>and since we can use <code>(+)</code> with Doubles we know they're a <code>Num</code> instance. And from there it's easy.</p> <pre><code>*&gt; 0 :: Double 0.0 </code></pre> <p>Last piece of the puzzle is <code>Double -&gt; Integer</code>. Well a brief search on Hoogle shows</p> <pre><code>truncate floor round -- etc ... </code></pre> <p>I'll leave that to you to search.</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.
    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