Basics

A good place to start is the Design Recipe. I've explained this on the page titled Coding Style, but really, it serves as a great example for the basics of programming in Scheme.

I'll write up the absolute basics so you have them to refer to.

Definitions and Interactions

20090111-drscheme

DrScheme has a Definitions area (top) and an Interactions area (bottom). If you want to experiment with something in the language, you can type it in the bottom half, hit return, and it will be immediately evaluated. If you want to define something (eg. write a program), write your code in the top half of the editor, and hit "Run" to execute your code.

A Prefix Syntax

In the Interactions pane, you can start by practicing using a prefix syntax for writing arithmetic expressions. For example, to add 3 and 5, I would not write 

3 + 5

Instead, in Scheme, I write

(+ 3 5)

The parentheses are not optional here—the opening paren tells me that the next thing I see will be an operator of some sort. Everything else in the parenthetical expression is an operand. In the Interactions pane, try typing (+ 3 5) and hit return; it should be evaluated to 8.

Definitions

Not everything that comes after an open-paren is an operator. Some things are actually syntaxes within the language. For example, if I want to define a value, I would write

(define some-var 8)

This defines the variable some-var to have the value 8.

I can also define a function:

(define (add-eight val)
  (+ val 8))


Defining a function is just a step away from defining a value. We start with

(define (... ...) ...)

We name the function

(define (add-eight ...) ...)

We name any parameters of that function

(define (add-eight var) ...)

and then we're ready to define the body of the function.

Function Invocation

If you write the code defined above in the Definitions pane, you can then Run it, and interact with it in the Interactions pane, below.

Try

(add-eight 10)

You should get 18. Functions are invoked by placing them in the operator position of a function application (just like +), and  passing the correct number of parameters to that function.

Too fast?

We'll spend plenty of time working with the language this semester, and there is time at the front of the semester for developing mastery. We'll move fast, but not too fast. At least, that's my goal.

Exercises

For exercises 1-... you may find section 2 of HtDP useful. These exercises are pulled directly from there.

  1. Say I want to compute the area of a ring. If I'm smart, I'll first write something that computes the area of a circle. Define a function called area-circle that takes one parameter, a radius, and returns the area of the circle.
  2. Define a function called area-ring that takes two parameters, the inner and outer radii of two circles, and returns the area of the ring. You should be able to compute this by subtracting the area of one circle from another.

It is common for students of algebra to encounter functions like the following:

You could express this in Scheme as

(define (f n)
  (+ (/ n 3) 2))


Do the same for the following expressions.

  1. n2 + 10
  2. (1/2) · n3 + 20 
  3. 2 - (1/n) 


With a partner, test each-other by coming up with some more examples. You may also want to explore the PLT Scheme Documentation to see what kind of mathematical functions exist. Can you take the sine or cosine of an angle? What about exponentiation? Scheme comments begin with a semicolon, so annotate your self-test exercises with some comments about things you discover along the way.

Save your work in a file called "first-day.scm" It may come in handy later.