mit-scheme is installed on tux.cs.drexel.edu. You can run it by typing mit-scheme.
You can download and install Scheme from http://www.gnu.org/software/mit-scheme/
To exit Scheme, type
To load a file in the current directory type
If the filename ends with .scm it will be appended automatically. To load maxmin.scm from the current directory
All function calls is scheme follow the pattern
(function_name arg1 arg2 arg3 ...)
Basic commands like addition, multiplication, etc also work in this way. This is called prefix notation.
1 ]=> (+ 2 3)
1 ]=> (- 5 7)
1 ]=> (* 6 7)
1 ]=> (< 1 5)
1 ]=> (< 5 1)
Lists are an important method for dealing with sets of data. A list is a set of elements in parentheses seperated by spaces.
To create a static list:
'(1 2 3 4 5)
To create a list where terms need to be evaluated use the list command:
(list 1 2 3 (+ 4 5))
The cons command adds an element to the from of the a list.
> (cons 1 '(2 3 4 5))
;Value 15: (1 2 3 4 5)
The car command selects the first element in a list.
> (car '(5 6 7 8))
The cdr command selects the tail of the list, everything but the first element.
> (cdr '(5 6 7 8))
;Value 16: (6 7 8)
We can find the end of the list by seeing if cdr is null.
> (cdr '(1))
The null? command is used to test if something is null
> (null? '())
In Scheme, we do not have while loops. We want to define everything as recursive functions.
The define command can be used for defining variables and functions.
(define L '(1 2 3))
(define (sqr x) (* x x))
Once a function is defined we can apply it to arguments.
> (sqr 10)
To define many functions we will need conditional expressions.
The if statement takes three inputs. If the first argument is true is returns the second otherwise is returns the third.
(if (< 1 3) 4 5)
The cond extends the if statement to an unlimited number of cases.
(cond ((< 4 5) 5) ((> 4 7) 10) (else 11) )
To define a function to determine the length of a list, we need a base case and recursive case.
Base: An empty list has size zero
Recursive: The length of the list is 1+ the length of the tail.
(define (mblength L) (if (null? L) 0 (+ 1 (mblength (cdr L))) ) )
> (mblength '(1 2 3 4 5))
( (lambda (x) (+ x x)) 7)
Some additional useful functions are:
let: defines local variables for use in a commmand.
(let ( (a 4) (b 7)) (+ a b))
let*: Extends the let command so variables can be used immediately in other variables definitions
(let* ( (a 4) (b (* a a)) ) (+ a b))
map: Applies a function to every element in a list.
(map abs '(-1 -2 -3 4 -5 6 7))
reduce: Combine all the elements of a list using a binary function. The second input to the function is the starting value.
(reduce + 0 '(1 2 3 4 5 6 7 8 9))
pair?: Returns true if the element is a list
(pair? '(1 2 3))