A quick introduction to types, including how to specify types of variables and functions.
String
, Number
, Boolean
, Image
.(-> [input types] [output type])
(Listof Number)
- or even (Listof Any)
There are two ways to specify a type: with inline or with the
colon (:
) on a separate line.
name : Type
notation in place of the plain name
(define WIDTH : Integer 400)
Be careful to put spaces on either side
of the colon. This method becomes [name : Type]
when used in
arguments to a function (see below).
(: name Type)
on a separate line before name
(: HEIGHT Integer)
(define HEIGHT 300)
You can use the [name : Type]
or (: name Type)
method to write the
type of a function as well. You should include the output type, just
like a signature.
[name : Type]
method: A function that takes in a number and puts
out a number plus 5:
(define (f [x : Number]) : Number
(+ x 5))
(: name Type)
method: A function that takes in a string and puts
out double its length:
(: g (-> String Number))
(define (g str)
(* 2 (string-length str)))
A function that takes in a number and puts out a that many repetitions of “word” (literally):
(define (h [num : Integer]) : (Listof String)
(make-list num "word")