define
keywordI use the words "function", "operator", and "program" almost interchangeably. All three words mean "something that takes in some information, does some computation, and produces some information as a result".
The word "operator" tends to be used for built-in arithmetic functions like addition, subtraction, multiplication, division, square root, and comparison. The word "program" tends to be used for functions you or I have defined. The word "function" applies equally to both. Don't worry too much about this distinction.
For example, consider the familiar arithmetic operators
"+
", "-
", etc. from grade school:
in the algebraic expression 3 + 4
, the
operator "+" operates on the two pieces of information "3"
and "4" and produces the result "7".
+, -, *, /
all take two or more numbers
and return a numbersqrt
takes one number and returns a
numberremainder
and modulo
(two
names for the same operator) both take two integers and
return an integer: they divide the first operand by the
second, returning not the quotient but the
remainder.Examples:
(sqrt (+ (* 3 (remainder 5 3)) (* 4
4)))
(sqrt 3 4)
sqrt
function only accepts one
parameter.pi
has the value 3.14159....e
has the value 2.71828182....define
keywordThe define
keyword is used in two common
ways: defining variables
and defining
functions.
true
and false
are the two
built-in boolean values.<, >, =, <=, >=
all take two
or more numbers and return a booleansymbol=?
takes two symbols and returns a
booleaneq?
takes two objects (numbers, symbols,
booleans, or anything else) and returns a booleanand
and or
each takes two or
more booleans and returns a booleannot
takes a boolean and returns a
booleanzero?
takes a
number and returns a boolean.(zero? x)
is just another way of saying
(= x 0)
.even?
and odd?
each takes an integer and returns a boolean.(even? x)
is just another way of saying
(zero? (remainder x 2))
.(odd? x)
is just another way of saying
(not (even? x))
.number?
, integer?
,
symbol?
, string?
,
boolean?
, and struct?
all take in an
object (of any type) and return a boolean.
Examples:
(or (<= price 10.00) (symbol=? my-symbol
'bluebird))
price
is
less than or equal to 10.00; if so, it returns
true
. If not, it then tests whether
my-symbol
is the symbol 'bluebird
; if
so, it returns true
, otherwise
false
.(or 3 4)
or
works on booleans,
not numbers(< true false)
(+ 3 (< 4 5))
(+ 3 true)
, because the value of (<
4 5)
is true
. The second step fails
because +
only works on numbers, not on a
number and a boolean.Scheme contains several conditional constructs, but the
only one we need in this course is cond
.
The
define-struct
keyword allows you to add new
aggregate data types to the language.
Note: to use any of these functions, you
need to load the image.ss
teachpack (or the
world.ss
teachpack, which includes all the same
stuff and more). See also Dr. Felleisen's exercises on
animation worlds.
See updated documentation for the image teachpack. (I'll add some of my own if I think it necessary.)
empty
is a variable, not a function; its
value is the empty list.cons
takes an object and a list, and
returns a new list with the specified object as its first
element, and the specified list as the rest. In Beginner
mode, it signals an error if the second parameter is not
a list.first
takes a non-empty list and returns
the first element. It signals an error message if it is
given an empty list or anything other than a list.rest
takes a non-empty list and returns
a list of everything but the first element. It signals an
error message if it is given an empty list or anything
other than a list.empty?
takes an object and returns a
boolean indicating whether it is the empty list.cons?
takes an object and returns a
boolean indicating whether it is a non-empty list.list
takes zero or more objects and returns a
list of them all. We'll start with cons
, which is
more unwieldy but conceptually simpler, and add the
list
function later in the workshop.Examples:
(cons 'bluebird empty)
(define bird-list (cons 'bluebird (cons 'mallard (cons 'crow empty)))
bird-list
to be a list of three symbols.bird-list
(cons 'bluebird (cons 'mallard (cons
'crow empty)))
(first bird-list)
'bluebird
(rest bird-list)
(cons 'mallard (cons 'crow
empty))
(first (rest bird-list))
'mallard
(rest (first bird-list))
(first bird-list)
returns the symbol
'bluebird
, but rest
can only be applied
to a non-empty list, not a symbol.'bluebird
is a symbol, whereas
(cons 'bluebird empty)
is a list whose one
element is a symbol.
(empty? bird-list)
false
(cons? bird-list)
true
(empty? (rest (rest (rest bird-list))))
true
(list 'bluebird 'mallard 'crow)
(cons 'bluebird (cons 'mallard (cons 'crow empty)))
.(list 'bluebird (list 'mallard (list 'crow
empty)))