Vocabulary of Minimal Scheme

Outline

Functions, Operators, and Programs

I 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".

Arithmetic operators
• +, -, *, / all take two or more numbers and return a number
• sqrt takes one number and returns a number
• remainder 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)
Illegal because the sqrt function only accepts one parameter.
Predefined numeric constants
• pi has the value 3.14159....
• e has the value 2.71828182....
The define keyword

The define keyword is used in two common ways: defining variables and defining functions.

Boolean operators
• true and false are the two built-in boolean values.
• <, >, =, <=, >= all take two or more numbers and return a boolean
• eq? takes two objects (numbers, symbols, booleans, or anything else) and returns a boolean
• and, or each takes two or more booleans and returns a boolean
• not takes a boolean and returns a boolean
• zero?, positive?, negative? all take a number and return a boolean.
(zero? x) is just another way of saying (= x 0).
(positive? x) is just another way of saying (> x 0).
(negative? x) is just another way of saying (< x 0).

Examples:

(or (<= price 10.00) (eq? my-symbol 'bluebird))
Legal. This determines whether 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)
Illegal because or works on booleans, not numbers
(< true false)
Illegal because < works on numbers, not booleans
(+ 3 (< 4 5))
The first step of simplification turns this into (+ 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.
Conditionals

Scheme contains several conditional constructs, but the only one we need in this course is cond.

Defining structures

The define-struct keyword allows you to add new aggregate data types to the language.

Graphics

Note: to use any of these, you need to load draw-lib.ss or one of the other libraries that uses it. These libraries include a "define-struct" for the "posn" structure, so if you're using them, don't write your own definition of "posn".

• start takes two numbers (width and height), creates a canvas width x height pixels, and returns true. Do this before using any of the following graphics calls.
• draw-solid-line takes two posns and (optionally) a color ('white, 'black, 'red, 'yellow, 'green, or 'blue); draws a line between the two posns in the specified color, and returns true.
• draw-solid-rect takes a posn and two numbers (width and height) and (optionally) a color; draws a solid rectangle whose upper left corner is the specified position and whose width and height are as specified, and returns true.
• draw-solid-disk takes a posn and a number and (optionally) a color; draws a solid disk whose center is the specified position and whose radius is the specified number, and returns true.
• draw-circle takes a posn and a number and (optionally) a color; draws the outline of a circle whose center is the specified position and whose radius is the specified number, and returns true.
• wait-for-mouse-click takes no parameters at all. It waits for the user to click the mouse button somewhere inside the graphics window, then returns the coordinates as a posn.

Examples: • From the Language menu, choose "Set Teachpack To..." and choose the file draw.ss.
• (start 200 200)
opens a graphics window 200 pixels square.
• (draw-solid-rect (make-posn 40 40) 120 120 BLUE)
draws a blue square with upper-left corner (40,40), 120 pixels on a side.
• (define center (make-posn 100 100))
doesn't draw anything, but defines a point for future use.
• (draw-solid-disk center 50 YELLOW)
draws a yellow disk centered at the point (overwriting part of the previous rectangle).
• (draw-solid-line (make-posn 100 50) (make-posn 100 150))
draws a black line down through the middle of the disk.
• (draw-solid-line center (make-posn 130 140))
draws a black radius down and to the right.
• (draw-solid-line center (make-posn 70 140))
draws another black radius down and to the left.
Lists
• 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.

Examples:

(cons 'bluebird empty)
returns exactly the same thing
(define bird-list (cons 'bluebird (cons 'mallard (cons 'crow empty)))
returns nothing, but defines bird-list to be a list of three symbols.
bird-list
returns (cons 'bluebird (cons 'mallard (cons 'crow empty)))
(first bird-list)
returns 'bluebird
(rest bird-list)
returns (cons 'mallard (cons 'crow empty))
(first (rest bird-list))
returns 'mallard
(empty? bird-list)
returns false
(cons? bird-list)
returns true
(empty? (rest (rest (rest bird-list))))
returns true