What does % do in Haskell?

  • A+
Category:Languages

I'm used to using % to mean "modulo" in other languages. In Haskell, we have to use mod x y or x `mod` y. So, what is this symbol used for in Haskell?

 


With a quick look on Hoogle, you can see that % is an infix function defined as

(%) :: Integral a => a -> a -> Ratio a 

and as you can guess it is part of the Data.Ratio library, which mostly deals with ratios (i.e.: fractions). It is code is

x % y = reduce (x * signum y) (abs y) 

thus given two integrals (x,y) , it returns an irreducible fraction x/y

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: