Scheme Tutorial - Textual Interaction

This portion will lead you through some simple text based interaction using the Scheme programming environments.  All the examples below can be done with either MIT Scheme or Dr. Scheme.  Again, this is not a language tutorial, so little explanation will be given about the syntax of the commands.   For all the code examples the output of the scheme interpreter is  blue and the user input is salmon.

 

Example 1: Defining and referencing a variable

From the command line in either MIT Scheme or either window in Dr. Scheme declare a list foo.  If you are using the declarations window of doctor scheme you will have to click the execute button, otherwise just press enter.  Type:

> ( define foo '(1 2 3))

The following operations examine and manipulate of. These should be typed into the interaction window of Dr. Scheme (entering them in the declarations window will give the same results)


;Value: foo

> foo

;Value 2: (1 2 3)

> (car foo)

;Value: 1

>
(cdr foo)

;Value 4: (2 3)

>
(caddr foo)

;Value: 3

 

Example 2: Creating a Simple Function

From the command line in either MIT Scheme or either window in Dr. Scheme declare a function c-to-f.  This function takes a single argument c and applies basic mathematical operations to covert c from degrees Centigrade to  degrees Fahrenheit If you are using the declarations window of doctor scheme you will have to click the execute button, otherwise just press enter.  In the declarations buffer of Dr. Scheme, notice how when you move the mouse cursor over a variable arrows appear to all other instances of that variable.  Type:

(define centigrade-to-fahrenheit
                  (lambda (c) (+ (* 1.8 c) 32)))

The following operations examine and manipulate foo. These should be typed into the interaction window of Dr. Scheme (although entering them in the declarations window will give the same results, these are in fact interactions, and thus the operation fit better into the interaction buffer)


> (centigrade-to-fahrenheit 0)

;Value: 32

>
(centigrade-to-fahrenheit 12.2 )

;Value: 53.96

 

Example 3: Creating a Recursive Function

For this example we will try to load a file.   Save sample.s into your working directory.   This file contains the The file contains the following code:

;; Our basic centigrade to Fahrenheit function
(define centigrade-to-fahrenheit
(lambda (c) (+ (* 1.8 c) 32)))

;; Takes a list l and recursively walks through converting each value
(define centigrade-to-fahrenheit-2
(lambda (l) (if (null? l) null
(cons (centigrade-to-fahrenheit (car l)) ( centigrade-to-fahrenheit-2 (cdr l))))))

;; list of temps
(define temps '(0 20 40 60 80 100))

In MIT Scheme type you must use the load command to load the file into the interpreter.   In Dr. Scheme you may either use the load command, or go to the File:Open menu.   If you use load in Dr. Scheme the working directory is the directory which you have selected for saving the buffers in. 


> ( centigrade-to-fahrenheit 88)


;Value: 190.4


>
( centigrade-to-fahrenheit-2 temps)


; Value: (list 32.0 68.0 104.0 140.0 176.0 212.0)

Back   Continue