1 Beginning Student
program | = | def-or-expr ... | ||
def-or-expr | = | definition | ||
| | expr | |||
| | test-case | |||
| | library-require | |||
definition | = | (define (name variable variable ...) expr) | ||
| | (define name expr) | |||
| | (define name (lambda (variable variable ...) expr)) | |||
| | (define-struct name (name ...)) | |||
expr | = | (name expr expr ...) | ||
| | (cond [expr expr] ... [expr expr]) | |||
| | (cond [expr expr] ... [else expr]) | |||
| | (if expr expr expr) | |||
| | (and expr expr expr ...) | |||
| | (or expr expr expr ...) | |||
| | name | |||
| | ’name | |||
| | number | |||
| | boolean | |||
| | string | |||
| | character | |||
test-case | = | (check-expect expr expr) | ||
| | (check-within expr expr expr) | |||
| | (check-member-of expr expr ...) | |||
| | (check-range expr expr expr) | |||
| | (check-error expr expr) | |||
| | (check-error expr) | |||
library-require | = | (require string) | ||
| | (require (lib string string ...)) | |||
| | (require (planet string package)) | |||
package | = | (string string number number) |
1.1 Pre-Defined Variables
value
empty : empty?
value
true : boolean?
value
false : boolean?
1.2 Template Variables
syntax
syntax
syntax
syntax
syntax
1.3 Syntax
syntax
(define (name variable variable ...) expression)
syntax
(define name expression)
A lambda cannot be used outside of this alternate syntax.
syntax
’name
syntax
(quote name)
syntax
(define-struct structure-name (field-name ...))
make-structure-name : takes a number of arguments equal to the number of fields in the structure, and creates a new instance of that structure.
structure-name-field-name : takes an instance of the structure and returns the value in the field named by field-name.
structure-name? : takes any value, and returns true if the value is an instance of the structure.
The name of the new functions introduced by define-struct must not be the same as that of other functions or variables, otherwise define-struct reports an error.
syntax
(name expression expression ...)
syntax
(cond [question-expression answer-expression] ...)
(cond [question-expression answer-expression] ... [else answer-expression])
If none of the question-expressions evaluates to true, cond’s value is the answer-expression of the else clause. If there is no else, cond reports an error. If the result of a question-expression is neither true nor false, cond also reports an error.
syntax
(if test-expression then-expression else-expression)
syntax
(and expression expression expression ...)
syntax
(or expression expression expression ...)
syntax
(check-expect expression expected-expression)
syntax
(check-within expression expected-expression delta)
It is an error for expressions or expected-expression to produce a function value.
syntax
(check-error expression expected-error-message)
(check-error expression)
syntax
(check-member-of expression expression expression ...)
syntax
(check-range expression low-expression high-expression)
syntax
(require string)
syntax
(require module-name)
syntax
(require (lib string string ...))
syntax
(require (planet string (string string number number)))
syntax
(require (planet id))
syntax
(require (planet string))
The full grammar for planet requires is given in Importing and Exporting: require and provide, but the best place to find examples of the syntax is on the the PLaneT server, in the description of a specific package.
1.4 Pre-defined Functions
1.5 Numbers: Integers, Rationals, Reals, Complex, Exacts, Inexacts
procedure
(* x y z ...) → number
x : number y : number z : number
> (* 5 3) 15
> (* 5 3 2) 30
procedure
(+ x y z ...) → number
x : number y : number z : number
> (+ 2/3 1/16) 35/48
> (+ 3 2 5 8) 18
procedure
(- x y ...) → number
x : number y : number
> (- 5) -5
> (- 5 3) 2
> (- 5 3 1) 1
procedure
(/ x y z ...) → number
x : number y : number z : number
> (/ 12 2) 6
> (/ 12 2 3) 2
procedure
(< x y z ...) → boolean?
x : real y : real z : real
> (< 42 2/5) false
procedure
(<= x y z ...) → boolean?
x : real y : real z : real
> (<= 42 2/5) false
procedure
(= x y z ...) → boolean?
x : number y : number z : number
> (= 42 2/5) false
procedure
(> x y z ...) → boolean?
x : real y : real z : real
> (> 42 2/5) true
procedure
(>= x y z ...) → boolean?
x : real y : real z : real
> (>= 42 42) true
procedure
(abs x) → real
x : real
> (abs -12) 12
procedure
(acos x) → number
x : number
> (acos 0) #i1.5707963267948966
procedure
(add1 x) → number
x : number
> (add1 2) 3
procedure
(angle x) → real
x : number
> (angle (make-polar 3 4)) #i-2.2831853071795867
procedure
(asin x) → number
x : number
> (asin 0) 0
procedure
(atan x) → number
x : number
> (atan 0) 0
> (atan 0.5) #i0.4636476090008061
> (atan 3 4) #i0.6435011087932844
> (atan -2 -1) #i-2.0344439357957027
procedure
(ceiling x) → integer
x : real
> (ceiling 12.3) #i13.0
procedure
(complex? x) → boolean?
x : any/c
> (real? 1-2i) false
procedure
(conjugate x) → number
x : number
> (conjugate (make-polar 3 4)) #i-1.960930862590836+2.2704074859237844i
procedure
(cos x) → number
x : number
> (cos pi) #i-1.0
procedure
(cosh x) → number
x : number
> (cosh 10) #i11013.232920103324
procedure
(current-seconds) → integer
> (current-seconds) 1390627788
procedure
(denominator x) → integer
x : rational?
> (denominator 2/3) 3
value
e : real
> e #i2.718281828459045
procedure
(even? x) → boolean?
x : integer
> (even? 2) true
procedure
(exact->inexact x) → number
x : number
> (exact->inexact 12) #i12.0
procedure
(exact? x) → boolean?
x : number
> (exact? (sqrt 2)) false
procedure
(exp x) → number
x : number
> (exp -2) #i0.1353352832366127
procedure
(expt x y) → number
x : number y : number
> (expt 16 1/2) 4
> (expt 3 -4) 1/81
procedure
(floor x) → integer
x : real
> (floor 12.3) #i12.0
procedure
(gcd x y ...) → integer
x : integer y : integer
> (gcd 6 12 8) 2
procedure
(imag-part x) → real
x : number
> (imag-part 3+4i) 4
procedure
(inexact->exact x) → number
x : number
> (inexact->exact 12.0) 12
procedure
(inexact? x) → boolean?
x : number
> (inexact? 1-2i) false
procedure
(integer->char x) → char
x : exact-integer?
> (integer->char 42) #\*
procedure
(integer-sqrt x) → integer
x : number
> (sqrt 11) #i3.3166247903554
procedure
(integer? x) → boolean?
x : any/c
> (integer? (sqrt 2)) false
procedure
(lcm x y ...) → integer
x : integer y : integer
> (lcm 6 12 8) 24
procedure
(log x) → number
x : number
> (log 12) #i2.4849066497880004
procedure
(magnitude x) → real
x : number
> (magnitude (make-polar 3 4)) #i3.0
procedure
(make-polar x y) → number
x : real y : real
> (make-polar 3 4) #i-1.960930862590836-2.2704074859237844i
procedure
(make-rectangular x y) → number
x : real y : real
> (make-rectangular 3 4) 3+4i
procedure
(max x y ...) → real
x : real y : real
> (max 3 2 8 7 2 9 0) 9
procedure
(min x y ...) → real
x : real y : real
> (min 3 2 8 7 2 9 0) 0
procedure
(modulo x y) → integer
x : integer y : integer
> (modulo 9 2) 1
> (modulo 3 -4) -1
procedure
(negative? x) → boolean?
x : number
> (negative? -2) true
procedure
(number->string x) → string
x : number
> (number->string 42) "42"
procedure
(number? n) → boolean?
n : any/c
> (number? "hello world") false
> (number? 42) true
procedure
(numerator x) → integer
x : rational?
> (numerator 2/3) 2
procedure
(odd? x) → boolean?
x : integer
> (odd? 2) false
value
pi : real
> pi #i3.141592653589793
procedure
(positive? x) → boolean?
x : number
> (positive? -2) false
procedure
(quotient x y) → integer
x : integer y : integer
> (quotient 9 2) 4
> (quotient 3 4) 0
procedure
(random x) → natural
x : natural
> (random 42) 14
procedure
(rational? x) → boolean?
x : any/c
> (rational? 1-2i) false
procedure
(real-part x) → real
x : number
> (real-part 3+4i) 3
procedure
(real? x) → boolean?
x : any/c
> (real? 1-2i) false
procedure
(remainder x y) → integer
x : integer y : integer
> (remainder 9 2) 1
> (remainder 3 4) 3
procedure
(round x) → integer
x : real
> (round 12.3) #i12.0
procedure
(sgn x) → (union 1 1.0 0 0.0 -1 -1.0)
x : real
> (sgn -12) -1
procedure
(sin x) → number
x : number
> (sin pi) #i1.2246467991473532e-16
procedure
(sinh x) → number
x : number
> (sinh 10) #i11013.232874703393
procedure
(sqr x) → number
x : number
> (sqr 8) 64
procedure
(sqrt x) → number
x : number
> (sqrt 9) 3
> (sqrt 2) #i1.4142135623730951
procedure
(sub1 x) → number
x : number
> (sub1 2) 1
procedure
(tan x) → number
x : number
> (tan pi) #i-1.2246467991473532e-16
procedure
(zero? x) → boolean?
x : number
> (zero? 2) false
1.6 Booleans
procedure
(boolean=? x y) → boolean?
x : boolean? y : boolean?
> (boolean=? true false) false
procedure
(boolean? x) → boolean?
x : any/c
> (boolean? 42) false
> (boolean? false) true
procedure
(false? x) → boolean?
x : any/c
> (false? false) true
procedure
(not x) → boolean?
x : boolean?
> (not false) true
1.7 Symbols
procedure
(symbol->string x) → string
x : symbol
> (symbol->string 'c) "c"
procedure
(symbol=? x y) → boolean?
x : symbol y : symbol
> (symbol=? 'a 'b) false
procedure
(symbol? x) → boolean?
x : any/c
> (symbol? 'a) true
1.8 Lists
procedure
(append x y z ...) → list?
x : list? y : list? z : list?
> (append (cons 1 (cons 2 empty)) (cons "a" (cons "b" empty))) (list 1 2 "a" "b")
procedure
(assq x l) → (union false cons?)
x : any/c l : list?
> a (list (list 'a 22) (list 'b 8) (list 'c 70))
> (assq 'b a) (list 'b 8)
procedure
(caaar x) → any/c
x : list?
> w (list (list (list (list "bye") 3) true) 42)
> (caaar w) (list "bye")
procedure
(caadr x) → any/c
x : list?
> z (list (list (list (list 'a 'b) 2 3)) (list false true) "world")
> (caadr z) false
procedure
(caar x) → any/c
x : list?
> y (list (list (list 1 2 3) false "world"))
> (caar y) (list 1 2 3)
procedure
(cadar x) → any/c
x : list?
> w (list (list (list (list "bye") 3) true) 42)
> (cadar w) true
procedure
(cadddr x) → any/c
x : list?
> v (list 1 2 3 4 5 6 7 8 9 'A)
> (cadddr v) 4
procedure
(caddr x) → any/c
x : list?
> x (list 2 "hello" true)
> (caddr x) true
procedure
(cadr x) → any/c
x : list?
> x (list 2 "hello" true)
> (cadr x) "hello"
procedure
(car x) → any/c
x : cons?
> x (list 2 "hello" true)
> (car x) 2
procedure
(cdaar x) → any/c
x : list?
> w (list (list (list (list "bye") 3) true) 42)
> (cdaar w) (list 3)
procedure
(cdadr x) → any/c
x : list?
> z (list (list (list (list 'a 'b) 2 3)) (list false true) "world")
> (cdadr z) (list true)
procedure
(cdar x) → list?
x : list?
> y (list (list (list 1 2 3) false "world"))
> (cdar y) (list false "world")
procedure
(cddar x) → any/c
x : list?
> w (list (list (list (list "bye") 3) true) 42)
> (cddar w) empty
procedure
(cdddr x) → any/c
x : list?
> v (list 1 2 3 4 5 6 7 8 9 'A)
> (cdddr v) (list 4 5 6 7 8 9 'A)
procedure
(cddr x) → list?
x : list?
> x (list 2 "hello" true)
> (cddr x) (list true)
procedure
(cdr x) → any/c
x : cons?
> x (list 2 "hello" true)
> (cdr x) (list "hello" true)
procedure
(cons x y) → list?
x : any/x y : list?
> (cons 1 empty) (cons 1 empty)
procedure
(cons? x) → boolean?
x : any/c
> (cons? (cons 1 empty)) true
> (cons? 42) false
procedure
(eighth x) → any/c
x : list?
> v (list 1 2 3 4 5 6 7 8 9 'A)
> (eighth v) 8
procedure
(empty? x) → boolean?
x : any/c
> (empty? empty) true
> (empty? 42) false
procedure
(fifth x) → any/c
x : list?
> v (list 1 2 3 4 5 6 7 8 9 'A)
> (fifth v) 5
procedure
(first x) → any/c
x : cons?
> x (list 2 "hello" true)
> (first x) 2
procedure
(fourth x) → any/c
x : list?
> v (list 1 2 3 4 5 6 7 8 9 'A)
> (fourth v) 4
procedure
(length l) → natural-number?
l : list?
> x (list 2 "hello" true)
> (length x) 3
procedure
(list x ...) → list?
x : any/c
> (list 1 2 3 4 5 6 7 8 9 0)
(cons
1
(cons
2
(cons
3
(cons
4
(cons
5
(cons 6 (cons 7 (cons 8 (cons 9 (cons 0 empty))))))))))
procedure
(list* x ... l) → list?
x : any/c l : list?
> x (cons 2 (cons "hello" (cons true empty)))
> (list* 4 3 x) (cons 4 (cons 3 (cons 2 (cons "hello" (cons true empty)))))
procedure
(list-ref x i) → any/c
x : list? i : natural?
> v (list 1 2 3 4 5 6 7 8 9 'A)
> (list-ref v 9) 'A
procedure
(make-list i x) → list?
i : natural-number x : any/c
> (make-list 3 "hello") (cons "hello" (cons "hello" (cons "hello" empty)))
procedure
(member x l) → boolean?
x : any/c l : list?
> x (list 2 "hello" true)
> (member "hello" x) true
procedure
(member? x l) → boolean?
x : any/c l : list?
> x (list 2 "hello" true)
> (member? "hello" x) true
procedure
(memq x l) → boolean?
x : any/c l : list?
> x (list 2 "hello" true)
> (memq (list (list 1 2 3)) x) false
procedure
(memq? x l) → boolean?
x : any/c l : list?
> x (list 2 "hello" true)
> (memq? (list (list 1 2 3)) x) false
procedure
(memv x l) → (or/c false list)
x : any/c l : list?
> x (list 2 "hello" true)
> (memv (list (list 1 2 3)) x) false
value
null : list
> null empty
procedure
(null? x) → boolean?
x : any/c
> (null? empty) true
> (null? 42) false
procedure
(range start end step) → list?
start : number end : number step : number
> (range 0 10 2) (cons 0 (cons 2 (cons 4 (cons 6 (cons 8 empty)))))
procedure
(remove x l) → list?
x : any/c l : list?
> x (list 2 "hello" true)
> (remove "hello" x) (list 2 true)
procedure
(rest x) → any/c
x : cons?
> x (list 2 "hello" true)
> (rest x) (list "hello" true)
procedure
(reverse l) → list
l : list?
> x (list 2 "hello" true)
> (reverse x) (list true "hello" 2)
procedure
(second x) → any/c
x : list?
> x (list 2 "hello" true)
> (second x) "hello"
procedure
(seventh x) → any/c
x : list?
> v (list 1 2 3 4 5 6 7 8 9 'A)
> (seventh v) 7
procedure
(sixth x) → any/c
x : list?
> v (list 1 2 3 4 5 6 7 8 9 'A)
> (sixth v) 6
procedure
(third x) → any/c
x : list?
> x (list 2 "hello" true)
> (third x) true
1.9 Posns
procedure
(make-posn x y) → posn
x : any/c y : any/c
> (make-posn 3 3) (posn 3 3)
> (make-posn "hello" true) (posn "hello" true)
procedure
(posn) → signature
procedure
(posn-x p) → any
p : posn
> p (posn 2 -3)
> (posn-x p) 2
procedure
(posn-y p) → any
p : posn
> p (posn 2 -3)
> (posn-y p) -3
procedure
(posn? x) → boolean?
x : any/c
> q (posn "bye" 2)
> (posn? q) true
> (posn? 42) false
1.10 Characters
procedure
(char->integer c) → integer
c : char
> (char->integer #\a) 97
> (char->integer #\z) 122
procedure
(char-alphabetic? c) → boolean?
c : char
> (char-alphabetic? #\Q) true
procedure
(char-ci<=? c d e ...) → boolean?
c : char d : char e : char
> (char-ci<=? #\b #\B) true
> (char<=? #\b #\B) false
procedure
(char-ci<? c d e ...) → boolean?
c : char d : char e : char
> (char-ci<? #\B #\c) true
> (char<? #\b #\B) false
procedure
(char-ci=? c d e ...) → boolean?
c : char d : char e : char
> (char-ci=? #\b #\B) true
procedure
(char-ci>=? c d e ...) → boolean?
c : char d : char e : char
> (char-ci>=? #\b #\C) false
> (char>=? #\b #\C) true
procedure
(char-ci>? c d e ...) → boolean?
c : char d : char e : char
> (char-ci>? #\b #\B) false
> (char>? #\b #\B) true
procedure
(char-downcase c) → char
c : char
> (char-downcase #\T) #\t
procedure
(char-lower-case? c) → boolean?
c : char
> (char-lower-case? #\T) false
procedure
(char-numeric? c) → boolean?
c : char
> (char-numeric? #\9) true
procedure
(char-upcase c) → char
c : char
> (char-upcase #\t) #\T
procedure
(char-upper-case? c) → boolean?
c : char
> (char-upper-case? #\T) true
procedure
(char-whitespace? c) → boolean?
c : char
> (char-whitespace? #\tab) true
procedure
(char<=? c d e ...) → boolean?
c : char d : char e : char
> (char<=? #\a #\a #\b) true
procedure
(char<? x d e ...) → boolean?
x : char d : char e : char
> (char<? #\a #\b #\c) true
procedure
(char=? c d e ...) → boolean?
c : char d : char e : char
> (char=? #\b #\a) false
procedure
(char>=? c d e ...) → boolean?
c : char d : char e : char
> (char>=? #\b #\b #\a) true
procedure
(char>? c d e ...) → boolean?
c : char d : char e : char
> (char>? #\A #\z #\a) false
procedure
(char? x) → boolean?
x : any/c
> (char? "a") false
> (char? #\a) true
1.11 Strings
procedure
(explode s) → (listof string)
s : string
> (explode "cat") (list "c" "a" "t")
procedure
(format f x ...) → string
f : string x : any/c
> (format "Dear Dr. ~a:" "Flatt") "Dear Dr. Flatt:"
> (format "Dear Dr. ~s:" "Flatt") "Dear Dr. \"Flatt\":"
> (format "the value of ~s is ~a" '(+ 1 1) (+ 1 1)) "the value of (+ 1 1) is 2"
procedure
(implode l) → string
l : list?
> (implode (cons "c" (cons "a" (cons "t" empty)))) "cat"
procedure
(int->string i) → string
i : integer
> (int->string 65) "A"
procedure
(list->string l) → string
l : list?
> (list->string (cons #\c (cons #\a (cons #\t empty)))) "cat"
procedure
(make-string i c) → string
i : natural-number c : char
> (make-string 3 #\d) "ddd"
procedure
(replicate i s) → string
i : natural-number s : string
> (replicate 3 "h") "hhh"
procedure
(string c ...) → string?
c : char
> (string #\d #\o #\g) "dog"
procedure
(string->int s) → integer
s : string
> (string->int "a") 97
procedure
(string->list s) → (listof char)
s : string
> (string->list "hello") (list #\h #\e #\l #\l #\o)
procedure
(string->number s) → (union number false)
s : string
> (string->number "-2.03") #i-2.03
> (string->number "1-2i") 1-2i
procedure
(string->symbol s) → symbol
s : string
> (string->symbol "hello") 'hello
procedure
(string-alphabetic? s) → boolean?
s : string
> (string-alphabetic? "123") false
> (string-alphabetic? "cat") true
procedure
(string-append s ...) → string
s : string
> (string-append "hello" " " "world" " " "good bye") "hello world good bye"
procedure
(string-ci<=? s t x ...) → boolean?
s : string t : string x : string
> (string-ci<=? "hello" "WORLD" "zoo") true
procedure
(string-ci<? s t x ...) → boolean?
s : string t : string x : string
> (string-ci<? "hello" "WORLD" "zoo") true
procedure
(string-ci=? s t x ...) → boolean?
s : string t : string x : string
> (string-ci=? "hello" "HellO") true
procedure
(string-ci>=? s t x ...) → boolean?
s : string t : string x : string
> (string-ci>? "zoo" "WORLD" "hello") true
procedure
(string-ci>? s t x ...) → boolean?
s : string t : string x : string
> (string-ci>? "zoo" "WORLD" "hello") true
procedure
(string-contains? s t) → boolean?
s : string t : string
> (string-contains? "at" "cat") true
procedure
(string-copy s) → string
s : string
> (string-copy "hello") "hello"
procedure
(string-ith s i) → 1string?
s : string i : natural-number
> (string-ith "hello world" 1) "e"
procedure
(string-length s) → nat
s : string
> (string-length "hello world") 11
procedure
(string-lower-case? s) → boolean?
s : string
> (string-lower-case? "CAT") false
procedure
(string-numeric? s) → boolean?
s : string
> (string-numeric? "123") true
> (string-numeric? "1-2i") false
procedure
(string-ref s i) → char
s : string i : natural-number
> (string-ref "cat" 2) #\t
procedure
(string-upper-case? s) → boolean?
s : string
> (string-upper-case? "CAT") true
procedure
(string-whitespace? s) → boolean?
s : string
> (string-whitespace? (string-append " " (string #\tab #\newline #\return))) true
procedure
(string<=? s t x ...) → boolean?
s : string t : string x : string
> (string<=? "hello" "hello" "world" "zoo") true
procedure
(string<? s t x ...) → boolean?
s : string t : string x : string
> (string<? "hello" "world" "zoo") true
procedure
(string=? s t x ...) → boolean?
s : string t : string x : string
> (string=? "hello" "world") false
> (string=? "bye" "bye") true
procedure
(string>=? s t x ...) → boolean?
s : string t : string x : string
> (string>=? "zoo" "zoo" "world" "hello") true
procedure
(string>? s t x ...) → boolean?
s : string t : string x : string
> (string>? "zoo" "world" "hello") true
procedure
(string? x) → boolean?
x : any/c
> (string? "hello world") true
> (string? 42) false
procedure
(substring s i j) → string
s : string i : natural-number j : natural-number
> (substring "hello world" 1 5) "ello"
> (substring "hello world" 4) "o world"
1.12 Images
procedure
(image=? i j) → boolean?
i : image j : image
> c1 > (image=? (circle 5 "solid" "green") c1) false
> (image=? (circle 10 "solid" "green") c1) true
procedure
(image? x) → boolean?
x : any/c
> c1 > (image? c1) true
1.13 Misc
procedure
(=~ x y z) → boolean?
x : number y : number z : non-negative-real
> (=~ 1.01 1.0 0.1) true
> (=~ 1.01 1.5 0.1) false
value
eof : eof-object?
> eof #<eof>
procedure
(eof-object? x) → boolean?
x : any/c
> (eof-object? eof) true
> (eof-object? 42) false
procedure
(eq? x y) → boolean?
x : any/c y : any/c
> (eq? (cons 1 empty) (cons 1 empty)) false
> one (list 1)
> (eq? one one) true
procedure
(equal? x y) → boolean?
x : any/c y : any/c
> (equal? (make-posn 1 2) (make-posn (- 2 1) (+ 1 1))) true
procedure
(equal~? x y z) → boolean?
x : any/c y : any/c z : non-negative-real
> (equal~? (make-posn 1.01 1.0) (make-posn 1.01 0.99) 0.2) true
procedure
(eqv? x y) → boolean?
x : any/c y : any/c
> (eqv? (cons 1 empty) (cons 1 empty)) false
> one (list 1)
> (eqv? one one) true
procedure
(error x ...) → void?
x : any/c
> zero 0
> (if (= zero 0) (error "can't divide by 0") (/ 1 zero)) can't divide by 0
procedure
(exit) → void
procedure
(identity x) → any
x : any/c
> (identity 42) 42
> (identity c1) > (identity "hello") "hello"
procedure
(struct? x) → boolean?
x : any/c
> (struct? (make-posn 1 2)) true
> (struct? 43) false