Introduction to Scheme

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 (quit) or (exit).
To load a file in the current directory type (load "filename").
If the filename ends with .scm it will be appended automatically. To load maxmin.scm from the current directory
(load "maxmin")

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)
;Value: 5
1 ]=> (- 5 7)
;Value: -2
1 ]=> (* 6 7)
;Value: 42
1 ]=> (< 1 5)
;Value: #t
1 ]=> (< 5 1)
;Value: #f

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))
;Value: 5

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))
;Value: ()

The null? command is used to test if something is null
> (null? '())
;Value: #t

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)
;Value: 100

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))
;Value: 5

The lambda command is used to define a function. This function does not need to be given a name.
( (lambda (x) (+ x x)) 7)
The define function syntax is just a shorthand for lambda commands. The above command creates a function and uses it immediately.

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? 5)
(pair? '(1 2 3))