Reading — step 1 of 5
Learn
~1 min readGetting Started
Standard arithmetic: + - * / . Crucial difference from most languages: / requires fractional types (Double, Float, etc.) and div does integer division:
-- 7 / 3 -- ✗ if both are Int — type error
7.0 / 3.0 -- 2.333... (Double)
7 `div` 3 -- 2
7 `mod` 3 -- 1
7 `quot` 3 -- 2 (rounds toward zero, like C's /)
7 `rem` 3 -- 1
The backticks turn a regular function into an infix operator. div and mod round toward negative infinity (mathematically nice). quot and rem round toward zero (matches C).
For power: ^ (Int exponent), ** (floating exponent), ^^ (rational base, Int exponent).
Conversion is explicit: fromIntegral n :: Double — Haskell never auto-promotes between numeric types.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…