Preview
Sign inSign up
Forms and builtinsNumbers

Forms and builtins

Numbers

36 builtins. Every example below ran on the evaluator, and the result shown is what it returned.

+

arity 0+
(+ n ...)

Sum of the arguments; (+) is 0.

(+ 1 2 3.5)

=>6.5

-

arity 1+
(- n m ...)
(- n)

Subtracts the rest from the first; negates a single argument.

(list (- 10 3 2) (- 5))

=>(5 -5)

*

arity 0+
(* n ...)

Product of the arguments; (*) is 1.

(* 2 3 4)

=>24

/

arity 1+
(/ n m ...)
(/ n)

Divides the first by the rest. Integers that divide evenly stay integers; otherwise the result is a float (there are no rationals).

(list (/ 10 2) (/ 1 4) (/ 2))

=>(5 0.25 0.5)

quotient

arity 2
(quotient n m)

Integer division, truncated toward zero.

(quotient 17 5)

=>3

remainder

arity 2
(remainder n m)

Remainder with the sign of the dividend.

(remainder -17 5)

=>-2

modulo

arity 2
(modulo n m)

Modulo with the sign of the divisor.

(modulo -17 5)

=>3

abs

arity 1
(abs n)

Absolute value.

(abs -7)

=>7

min

arity 1+
(min n ...)

Smallest argument.

(min 3 1 2)

=>1

max

arity 1+
(max n ...)

Largest argument.

(max 3 1 2)

=>3

expt

arity 2
(expt base power)

base raised to power. Exact for an integer base and non-negative integer power, a float otherwise.

(expt 2 100)

=>1267650600228229401496703205376

sqrt

arity 1
(sqrt n)

Square root. Exact for perfect squares.

(list (sqrt 16) (sqrt 2))

=>(4 1.4142135623730951)

(exact->inexact n)

Converts to a float.

(exact->inexact 1)

=>1.0

inexact

arity 1
(inexact n)

Converts to a float.

(inexact 1)

=>1.0

(inexact->exact n)

Converts an integral float to an integer.

(inexact->exact 3.0)

=>3

exact

arity 1
(exact n)

Converts an integral float to an integer.

(exact 3.0)

=>3

round

arity 1
(round n)

Rounds to the nearest integer, ties to even.

(list (round 2.5) (round 3.5) (round 2.6))

=>(2.0 4.0 3.0)

floor

arity 1
(floor n)

Largest integer not above the argument.

(floor 2.7)

=>2.0

ceiling

arity 1
(ceiling n)

Smallest integer not below the argument.

(ceiling 2.1)

=>3.0

truncate

arity 1
(truncate n)

Drops the fractional part.

(truncate -2.7)

=>-2.0

number?

arity 1
(number? x)

Is the argument a number?

(list (number? 1) (number? "1"))

=>(#t #f)

integer?

arity 1
(integer? x)

Is the argument an integer (including integral floats)?

(list (integer? 2) (integer? 2.0) (integer? 2.5))

=>(#t #t #f)

exact?

arity 1
(exact? n)

Is the argument an exact number (an integer)?

(list (exact? 1) (exact? 1.0))

=>(#t #f)

inexact?

arity 1
(inexact? n)

Is the argument a float?

(inexact? 1.0)

=>#t

zero?

arity 1
(zero? n)

Is the argument zero?

(zero? 0)

=>#t

positive?

arity 1
(positive? n)

Is the argument above zero?

(positive? 5)

=>#t

negative?

arity 1
(negative? n)

Is the argument below zero?

(negative? 5)

=>#f

even?

arity 1
(even? n)

Is the integer even?

(even? 10)

=>#t

odd?

arity 1
(odd? n)

Is the integer odd?

(odd? 10)

=>#f

=

arity 1+
(= n m ...)

Are the arguments equal?

(= 1 1.0)

=>#t

<

arity 1+
(< n m ...)

Are the arguments increasing?

(< 1 2 3)

=>#t

>

arity 1+
(> n m ...)

Are the arguments decreasing?

(> 3 2 2)

=>#f

<=

arity 1+
(<= n m ...)

Are the arguments non-decreasing?

(<= 1 2 2)

=>#t

>=

arity 1+
(>= n m ...)

Are the arguments non-increasing?

(>= 3 3 1)

=>#t

(number->string n)

Prints a number.

(number->string 42)

=>"42"

(string->number s)

Parses a number, or returns #f.

(list (string->number "42") (string->number "nope"))

=>(42 #f)

esc