# How to program in Scheme

## History of scheme

Scheme programming language started with development of earlier programming languages such as Lisp and ALGOL.

Lisp was invented in the MIT University by John McCarthy in 1958 and ALGOL was created in 1958 at ETH Zurich in a meeting of European and American computer scientists.

Scheme language took its syntax from Lisp, and lexical scope and block structure from ALGOL. In 1971 Sussman, Drew McDermott, and Eugene Charniak had developed Micro-Planner.
Since there were problems in the Planner, Hewitt and his students invented the Actor model of computation in 1972.

Steele and Sussman then wrote a Lisp Interpreter using Maclisp and then added mechanisms to it to understand Carl Hewitt’s Actor model. They decided to try model Actors in the lambda calculus. They called their modelling system Schemer since there was a tradition in calling lisp-driver languages such as Planner or Conniver. Scheme was called Schemer at the beginning which was stand for Scheme Programmer. [1]

## What is Scheme

Scheme is a functional programming language which is similar to the other Lisp programming language and is based on S-Expressions. A scheme program consists of nested lists which are main data structures in this language. A good feature of scheme programming language is that codes can easily be created and executed dynamically. Scheme contains a set of list processing functions such as cons, car and cdr which can be used to process lists and produce new lists. [2]

Scheme is a very simple language; it is easy to use lambda calculus to derive much of the syntax of the language from more primitive forms. [2]

## What is an S-Expression?

S-Expression is a list based data structure. It can be a nested list of other S-Expressions and represented in text by parenthesized. Atoms are strings of characters and all atoms are S-Expressions, so all atoms and lists are S-Expressions [3].

Here are examples of S-Expresions:

abc atoms are S-Expressions
(a b c)                 lists are S-Expressions
(a (b) c)               nested lists are S-Expressions

An S-Expression can be a string, a symbol, a number, a boolean, a char, or a list of S-expressions.

It is possible to use sexp? function to check if the given value is S-Expression:

(require 2htdp/universe)
> (sexp? ‘abc)

#t

> (sexp? “a”)

#t

> (sexp? ‘(a (b) c))

#t

Description of the different programming paradigms

There are 3 programming paradigms including: Functional ProgrammingProcedural   Programming and Object-oriented Programming.

1. Functional Programming

Functions, not objects or procedures, are the fundamental building blocks of a program. So programs are designed by the composition of functions. SCHEME, Lisp, HOPE and ML are examples of functional programming. Functional programming has functions to process lists easily [4].

1. Imperative programming (Procedural Programming)

A program is a series of instructions which operate on variables.  It is also known as procedural programming. FORTRAN, ALGOL, Pascal, C, MODULA2, Ada and BASIC are examples of procedural programming [5].

1. Object-oriented Programming

Object oriented programming is characterised by the defining of classes of objects, and their properties. It is possible to Inheritance of properties to reducing the amount of programming. Java and C++ are examples of object-oriented programming [6].

Example code snippets to demonstrate the principal differences

• Functional Programming:(define sum
(lambda(x y)
(+ x y)))

> (sum 1 2)

It is possible to apply a function to a list easily.
Here is a sample to add VAT to a list of numbers:

(define add-vat
(lambda (x)
(+ (* x 0.175) x)))

(map add-vat ‘(100 200 150 120 130))

Result:
(117.5 235.0 176.25 141.0 152.75)

• Object oriented programming:

class MyMath
{
public MyMath()
{
}
public int sum(int x, int y)
{
return x+y;
}
}

public class Main{
public static void main(String args[])
{
MyMath math = new MyMath();
System.out.println(MyMath.sum(1,2));
}
}

• Imperative Programming

int sum(int x, int y)
{
return x + y;
}

int main(void) {
int c = sum(12, 230);
printf(“%d “, c);

}

Advantages & disadvantages of functional programming

• Advantages:
The major advantage is that a program written in a functional manner is easy to understand and functions are easily reusable.
Since it is possible to develop large software’s which consists of many functions, it is easily possible to test all the functions separately and their results. It is easy to write the program with few lines of code while doing the same takes a lot of coding using other programming languages, The reason is that you can easily map a function to a list and use list processing functions to produce the output.
Functional programming encourages safe ways of programming and also it is easy to catch exceptions and errors [4].
• Disadvantages:
The major disadvantage of functional programming is that it is difficulty of doing input-output because this is inherently non functional.
Another disadvantage is that functional programming is not widely used [4].

References:

1. History of the Scheme, [Online], Available at:http://en.wikipedia.org/wiki/History_of_the_Scheme_programming_language [20 May 2010]
2. Scheme programming language, [Online], available at:http://en.wikipedia.org/wiki/Scheme_%28programming_language%29 [22 May 2010]
3. S-Expression, [Online], available at: http://en.wikipedia.org/wiki/S-expression  [22 May 2010]
4. Functional Programming, [Online], Available at: http://c2.com/cgi/wiki?FunctionalProgramming [19 May 2010]
5. Imperative Programming, [Online], Available at:http://en.wikipedia.org/wiki/Imperative_programming [22 May 2010]
6. Object Oriented Programming, [Online], Available at:
http://en.wikipedia.org/wiki/Object-oriented_programming [22 May 2010]
7. Programming paradigms, [Online], Available at:http://www.comp.glam.ac.uk/pages/staff/efurse/teaching/pp/Introduction.html [20 May 2010]
8. Functional Programming, [Online], Available at: http://c2.com/cgi/wiki?AdvantagesOfFunctionalProgramming [20 May 2010]

## About majid

Software engineer, Web developer and IT graduate. Profile: View My Profile
This entry was posted in Scripts. Bookmark the permalink.