An Introduction to Collective Algorithmic Music Composition

An Introduction to Scheme: Hello World!

The most powerful programming language is Lisp. If you don't know Lisp (or its variant, Scheme), you don't know what it means for a programming language to be powerful and elegant. Once you learn Lisp, you will understand what is lacking in most other languages. -Richard Stallman

The Lisp Programming Language

In 1960, John McCarthy published a paper of his design of a new programming language. McCarthy chose to name this language Lisp, an acronym for "List Processing". In Lisp, a list data structure is used for both code and data. Lisp has become a popular language for computer-assisted music composition systems. Among the most used Lisp dialects are Common Lisp, Clojure and Scheme.

Scheme is a functional programming language created at the MIT Artificial Intelligence Lab by Guy Steele and Gerald Sussman. It is one of the main dialects of LISP and it's commonly used as a scripting language for a variety of applications ranging from computer-aided music composition to image processing.

Your first program: Hello World!

We will use Scripthica to make our first script. This script will show an alert that will display the "Hello World!" message.

  1. Go to http://scripthica.com
  2. Select the lambda.
  3. Type the following code in the editor or terminal:
    (display "Hello World!")
    
  4. Click the Run button.

Congratulations! You just wrote your first program with the Scheme programming language. That was fun and easy right? Now lets create a function so it displays an alert for any string given as a parameter.

Syntax

Scheme relies on parenthesis and prefix notation as its main syntactic characteristics. Prefix notation implies that an operation is declared first than its operands. For example, instead of writing 3 + 2, in Scheme you would write it like this:

(+ 3 2)

Comments

To write a comment in Scheme, we use the semicolon. Including comments in our code will make it easier for us and others to understand what is going on with our program.

; this is a comment

Defining variables

In Scheme, the define keyword is used to assign a name to data.

(define a 5)

To change a value we can use set!:

(set! a 5)

Quote

To return an expression as data uso quote. Instead of evaluating an expression, it will just return as data literal.

; return as data literal
(quote x)
; => x

; another way to do the same thing:
'x
; => x

; example to better understand quote
; assign 1 to y
(define y 1)
y
; => 1
'y
; => y

Functions

A function is basically a block of code that will be executed when it's called. In Scheme, we can define a function in the following manner:


; this function takes a name and displays it in an alert
(define (hello name)
    (alert (string-append "hello " name)))

; another way to define functions is using a lambda
(define hello (lambda (name)
    (alert (string-append "hello " name))))

; To call the function, we simply type the name of the function
; and put a parameter in it:
(hello "Mrs. Stranger")