2. Scheme – An Overview

Scheme is a functional language, similiar to LISP. Some people are discouraged by its heavy use of parentheses. It is helpful when your favourite editor helps counting them. The GIMP uses the SIOD (Scheme In One Defun) interpreter as the basis for Script-Fu. Here is a short introduction in Scheme that ignores the functional approach and some minor details.

2.1. Function calls

Function calls can look this way:

(function arg1 arg2 arg3 ... argN)
(function1 arg1 (funktion2 arg2 arg3) arg4)
(+ 2 3 (/ 12 4))
	  

2.2. Function definition (typical)

The following code defines a function with three input arguments. The result of the function is the result of the last command. the let* opens a new scope for variables.

(define (newfunc arg1 arg2 arg3)
   (let* ((foo (+ arg1 5))
          (bar 0)
          (baz (+ arg1 arg2 arg3))
         )
      (set! bar (* arg2 arg3))
      (- foo bar baz)
   )
)
	  

It is common practice to define the variables used in a function in a let*-Block. You can assign a value to a variable with set!.

2.3. Control structures

(begin
   (func1 arg1 arg2)
   (func2 arg4 arg5)
)

(if (< a b)
   (then-stuff arg1 a b)
   (else-stuff arg2 b a foo)
)

(while (> foo bar)
   (func1 arg foo)
   (func2 bar baz)
)

(set! i 0)
(while (< i 10)
   (func foo i bar)
   (set! i (+ i 1))
)
	  

2.4. Lists

You can create lists with the list command or use '( ... ) as a shortcut. Usually you use car to access the first element and cdr to access the list without the first element:

-> (car (list 1 2 3))
  1
-> (cdr (list 1 2 3))
  (2 3)
-> (cdr '(1 2 3))
  (2 3)
-> (car (cdr (list 1 2 3)))
  2
-> (cadr '(1 2 3))
  2
-> (nth 2 '(1 2 3))
  3
-> (length '(1 2 3))
  3
	  

2.5. Arrays

Although scheme depends heavily on lists as its primary data structuring mechanism, arrays are also used for the purposes of The GIMP. You can access them this way:

-> (set! a (cons-array 4 'byte))
  #4"00000000"
-> (aset a 2 42)
  42
-> (aset a 3 23)
  23
-> (aref a 1)
  0
-> (aref a 3)
  23