**Table of Contents**

1. Scheme Functions and Constants

2. Scheme Structures

3. Scheme Defining Structures

4. Scheme Conditonals

5. Scheme Lists

6. Scheme Local Definitions

Scheme Functions

Scheme, is a functional programming language. A functional programming language is based on the use of functions and computing new values instead of mutating old variables. One of the main features of functional programming languages is functions are values. For this reason, functions can be parameters to a function and a function can also produce a function.

As we will see this has many practical issues in programming including simulating structures and creating abstract functions.

Creating Functions

We create functions using the define keyword.

Example:

(define (add-numbers x y) (+ x y))

This function is called add-numbers and takes 2 parameters x and y and produces the value (+ x y). Scheme uses prefix notation instead of infix notation as most modern languages use. This means that the operator comes before the operands.

For example,

(+ 3 2) is equivalent to 3 + 2.

In Scheme, the parentheses around an expression are required. They are used to indicate an expression not to dictate order of operations. An expression in general takes the form (operation operand1 operand2 .... operationx) when operation takes x parameters. A note the + operator takes an unlimited number of parameters.

Order of Operations

The parenthesis is required and expressions in the inner most expressions are evaluated first and then parenthesis are not required for order of operations.

Example:

(* (+ (- 5 3) 2) 4)

The expression (- 5 3) is evaluated first which produces the value 2. Then the expression becomes

(* (+ 2 2) 4)

Then (+ 2 2) is evaluated next and then the expression becomes (* 4 4) which simplifies to 16.

This makes the value of the entire expression 16 when fully evaluated.

Constants

Scheme DOES NOT support the use of variables. The reasoning here is functional programming is based on the use of recursion to make new values not mutate old ones. However, sometimes we may desire variables to make code cleaner. In this case what do we do?

Consider this code:

(define (grade-calculation mark1 mark2 mark3) (+ (* mark1 0.40) (* mark2 0.20) (* mark3 0.40)))

This function calculates a weighted mark of three marks. It works but what is the problem? The problem here is the code doesn’t explain nicely what 0.4 and 0.2 represent. Is the first 0.4 the same as the second 0.4 or are they different contexts? One could be assignments are worth 40% and the other could be exams are 40%. We just don’t know from the context the meaning of these numbers. We can solve this problem using constants.

Constants are defined as follows:

(define CONSTANT_NAME CONSTANT_VALUE)

So we can rewrite the above function as:

(define assignment-weight 0.4) (define exam1-weight 0.2) (define exam2-weight 0.4) (define (grade-calculation mark1 mark2 mark3) (+ (* mark1 assignment-weight) (* mark2 exam1-weight) (* mark3 exam2-weight)))

This function is so much easier to understand with the constants added. When we need to modify the program it is easy to modify since all our constants will be declared in one location (preferably at the top of the code file).

It is important to realize that the constants MUST be declared outside the function. You can only have ONE statement in your function. We will see later ways to get around this problem.

As always, post if you have ANY questions.

**Table of Contents**

1. Scheme Functions and Constants

2. Scheme Structures

3. Scheme Defining Structures

4. Scheme Conditonals

5. Scheme Lists

6. Scheme Local Definitions

**Edited by chili5, 21 August 2011 - 06:10 AM.**