Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท Syntax and Evaluation

Numbers and Booleans

Work with numbers and booleans in Racket and Scheme: the numeric tower, exact versus inexact values, arithmetic and comparison functions, and truthiness rules.

Numbers are the first values you work with, and Racket's number system is richer than most languages': integers never overflow, fractions stay exact, and floating-point is a separate, clearly marked kind. Booleans are simple, but Lisp has an important rule about truthiness that differs from C-like languages. This lesson covers both.

The stand-in interpreter used for Run uses JavaScript numbers, so it does not model exact fractions or big integers; those behaviours are marked as real Racket only.

The numeric tower

Racket arranges numbers in a tower from most to least precise:

Kind Examples Predicate
Integer 42, -7, 100000000000000000000 integer?
Rational (exact fraction) 1/3, -22/7 rational?
Real (inexact, floating-point) 3.14, 1e10, +inf.0 real?
Complex 3+4i number?
RACKET
> (/ 1 3)
1/3
> (/ 1.0 3)
0.3333333333333333
> (* 99999999999 99999999999)
9999999999800000000001
> (exact->inexact 1/3)
0.3333333333333333
> (sqrt 16)
4
> (sqrt 2)
1.4142135623730951

Integers have unlimited size (no overflow), and dividing two integers gives an exact fraction rather than rounding. A number with a decimal point is inexact and is stored as a floating-point number. Mixing them gives an inexact result.

In real Racket, (/ 1 3) is exactly 1/3. The stand-in on this page gives 0.3333333333333333 because it has no exact fractions. We avoid fraction output in the Run examples.

Arithmetic

RACKET
(+ 1 2 3)        ; 6        any number of arguments
(- 10 3)         ; 7
(- 5)            ; -5       one argument negates
(* 2 3 4)        ; 24
(/ 12 4)         ; 3        exact division
(quotient 17 5)  ; 3        integer division
(remainder 17 5) ; 2        sign follows the first argument
(modulo -7 3)    ; 2        sign follows the second argument
(expt 2 10)      ; 1024
(abs -9)         ; 9
(min 4 2 8)      ; 2
(max 4 2 8)      ; 8
(add1 5)         ; 6        racket: add1 and sub1
(sub1 5)         ; 4
Arithmetic functions

Notice modulo and remainder differ for negative numbers: (modulo -7 3) is 2 while (remainder -7 3) is -1.

Comparison

Comparison functions are also ordinary functions and take any number of arguments:

RACKET
(= 3 3)        ; #t
(< 1 2 3 4)    ; #t   chained: 1<2, 2<3, 3<4
(< 1 3 2)      ; #f
(>= 5 5 4)     ; #t
(zero? 0)      ; #t
(even? 10)     ; #t
(odd? 10)      ; #f
(positive? -1) ; #f
(integer? 3.0) ; #t   3.0 is an integer-valued number
(exact? 3.0)   ; #f

(< 1 2 3 4) asks whether the numbers are strictly increasing, which replaces the awkward 1 < 2 && 2 < 3 && 3 < 4.

Comparing numbers

Floating-point surprises

Inexact numbers behave as they do in every language:

RACKET
> (+ 0.1 0.2)
0.30000000000000004
> (= (+ 0.1 0.2) 0.3)
#f

For money and other exact work, prefer exact rationals: (+ 1/10 2/10) is exactly 3/10 in real Racket. Use inexact numbers for measurements, and compare them with a tolerance such as (< (abs (- a b)) 1e-9).

Booleans and truthiness

The two boolean values are #t (true) and #f (false). Racket also accepts #true and #false.

The most important rule: in a condition, only #f is false. Everything else is true, including 0, "", and '():

RACKET
(if 0 "true" "false")      ; "true"
(if "" "true" "false")     ; "true"
(if '() "true" "false")    ; "true"
(if #f "true" "false")     ; "false"

This is a common source of bugs when you come from Python, JavaScript or C, where 0 and the empty string count as false.

Only #f is false

and, or, not

not flips a boolean: only #f becomes #t. and and or are special forms that return one of their arguments, not just #t or #f:

RACKET
(and 1 2 3)      ; 3     the last value if everything is true
(and 1 #f 3)     ; #f    the first false value
(or #f #f 7)     ; 7     the first true value
(or #f #f)       ; #f
(or (assoc 'b '((a 1) (b 2))) 'none)   ; '(b 2)

Because any non-#f value is true, or is often used to supply a default: (or user-value default-value).

and and or return values

Predicates

By convention, a function that answers a yes/no question ends in ?: number?, zero?, null?, string?, procedure?. This is a naming convention, not syntax, but it appears everywhere and makes code read naturally: (if (null? items) ...).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In (if 0 "yes" "no"), what is returned?

    Only #f counts as false. 0 is true.
  2. 2. What does (< 1 2 3) check?

    Comparison functions accept any number of arguments and check them as a chain.
  3. 3. What does (or #f #f 7) return?

    or returns the first non-#f argument, here 7, not just #t.
  4. 4. In real Racket, what is (/ 1 3)?

    Dividing exact integers produces an exact rational. Use a decimal point (1.0) for floating point.

Summary

  • Racket numbers form a tower: unlimited-size integers, exact rationals, inexact floating-point, complex.
  • Arithmetic and comparison functions take any number of arguments; modulo and remainder differ for negatives.
  • Decimal points make numbers inexact; floating-point has the usual surprises.
  • Only #f is false. 0, "" and '() are true.
  • and and or return one of their arguments, which makes them useful for defaults.

Next: Defining Names with define and let