На этом шаге мы рассмотрим основные арифметические функции.
Приведём примитивы и встроенные функции языка Haskell, определённые в стандартном модуле Prelude.hs.
Перечислим основные арифметические функции.
abs:: Num a => a -> a abs n
> abs 4/6 > abs 0 > abs (-398) > abs (-0.2345) 0.66666666666667 0 398 0.2345
1, если x>0; sgn(x) ↔ -1, если x<0; 0, если x=0; signumReal:: Num a => a -> a signumReal n
> signumReal 23.4 > signumReal 0 > signumReal (-1111) 1 0 -1
> signum 14/3 > signum 0 > signum (-0.32587) 1.0 0 -1.0
sqrt:: Floating a => a -> a sqrt n
> sqrt 5 > sqrt 4.444 > sqrt 9 > sqrt 0 2.23606797749979 2.10807969488822 3.0 0.0
exp:: Floating a => a -> a exp n
> exp 0 > exp 1 > exp 2 > exp 2.7 1.0 2.71828182845905 7.38905609893065 14.8797317248728
log:: Floating a => a -> a log n
> log 1 > log 2 > log 2.222 0.0 0.693147180559945 0.798407691217438
subtract:: Num a => a -> a -> a subtract m n
> subtract 2 3 > subtract (-2) (-3) > subtract 1.0 1.9202 1 -1 0.9202
div:: Integral a => a -> a -> a div m n
> div 2 3 > div 5 2 > div (-18) (-2) > div 10 (-2) 0 2 9 -5
mod:: Integral a => a -> a -> a mod m n
> mod 2 3 > mod 5 2 > mod (-18) (-2) > mod 10 (-2) 2 1 0 0 > mod 5 (-2) -1
gcd:: Integral a => a -> a -> a gcd m n
> gcd 23 20 > gcd 12 16 > gcd (-18) (-36) > gcd 30 (-100) 1 4 18 10 > gcd 50 0 50
lcm:: Integral a => a -> a -> a lcm m n
> lcm 23 20 > lcm 12 16 > lcm (-18) (-36) > lcm 30 (-100) 460 48 36 300 > lcm 0 35 > lcm 23 0 0 0
numerator:: Integral a => Ratio a -> a numerator n
> numerator (2/3) > numerator (-5/3) > numerator (10/5) 2 -5 2
denominator:: Integral a => Ratio a -> a denominator n
> denominator (4/5) > denominator (-8/3) > denominator (8/4) 5 3 1
reduce:: Integral a => a -> a -> Ratio a reduce m n
> reduce 10 5 > reduce (-25) 30 > reduce 36 (-12) 2 % 1 -5 % 6 4 % -1
truncate:: (RealFrac a, Integral b) => a -> b truncate n
> truncate 234.56789 > truncate (-0.56789) > truncate 12 234 0 12
round:: (RealFrac a, Integral b) => a -> b round n
> round 2.56789 > round (-123.46789) > round (-123.54) 3 -123 -124
floor:: (RealFrac a, Integral b) => a -> b floor n
> floor 23.6207 > floor (-111.4321) > floor 8253 23 -112 8253
ceiling:: (RealFrac a, Integral b) => a -> b ceiling n
> ceiling 23.6207 > ceiling (-111.4321) > ceiling 8253 24 -111 8253
floatProperFraction:: (RealFrac a, Integral b) => a -> (b, a) floatProperFraction
> floatProperFraction 5.5 > floatProperFraction 27.28 (5,0.5) (27,0.280001)
properFraction n
> properFraction 5.5 > properFraction 27.28 (5,0.5) (27,0.280000000000001)
На следующем шаге мы рассмотрим функции приведения к типу.