Scheme

Last updated on 2025-07-01 | Edit this page

Estimated time: 40 minutes

Overview

Questions

  • How can I write a scheme program

Objectives

  • Learn to write a scheme program

Introduction


We will follow the introduction to scheme available at https://try.gambitscheme.org/.

This section will likely be updated. Encourage learners to type along with you, rather than hitting the run button. Do leave some time for discussion of the material.

Basic

SCHEME

(display "hello world!\n")

SCHEME

(+ 6 1)

SCHEME

(expt 2 8)

SCHEME

(* 4 (atan 1))

SCHEME

(define pi (* 4 (atan 1)))
(define circum (lambda (r) (* 2 pi r)))
(circum 5)

SCHEME

(import (srfi 48))
(format #t "1+2=~s\n" (+ 1 2))

SCHEME

(import (srfi 19))  ;; for time procedures
(import (srfi 48))  ;; for format

(define (get-date)
  (time-utc->date (current-time time-utc)))

(format #t
        "current UTC time: ~a\n"
        (date->string (get-date)))

Types

SCHEME

(string-append "A" "BC")

SCHEME

(string-ref "XYZ" 2)  ;; indexing

SCHEME

(string-length "AB")  ;; get length

SCHEME

(char->integer #\A)   ;; conversion

SCHEME

(char<? #\A #\Z)      ;; comparison

SCHEME

(expt 2 75)           ;; 2^75

SCHEME

(exp (* 75 (log 2)))  ;; e^(75*ln(2))

SCHEME

(* (/ 5 3) (/ 9 2))   ;; 5/3 * 9/2

SCHEME

(+ (sqrt -6.25) 1)    ;; sqrt(-6.25)+1

SCHEME

(> 1e-2 1)            ;; 0.01 > 1 ?

SCHEME

(define lst (list 1 2))
lst

SCHEME

(cons 0 lst)         ;; add to front

SCHEME

(append lst '(3 4))  ;; concatenation

SCHEME

(car lst)            ;; get first

SCHEME

(cdr lst)            ;; remove first

SCHEME

(cdr (cdr lst))      ;; remove both

SCHEME

(length lst)         ;; get length

SCHEME

(define v (make-vector 5 42))
v

SCHEME

(vector-set! v 2 #t)  ;; mutation
v

SCHEME

(vector-ref v 2)      ;; indexing

SCHEME

(vector-length v)     ;; get length

Challenge 1: Can you do it?

What is the difference between a list and a vector in scheme?

Lists are not indexed, one accesses the first and last items of a list. Vectors are indexed, any item can be accessed, but they are more expensive to process. Scheme is a programming language in the lisp family of programming languages (https://en.wikipedia.org/wiki/Lisp_(programming_language)), which extensively use lists.

SCHEME

(string->symbol "foo")  ;; conversion

SCHEME

(symbol->string 'foo)   ;; conversion

SCHEME

(define x (list '* 2 3 7))
x
(eval x)

Challenge 2: Can you do it?

Define an s-expression that uses the symbol + in a list that adds three numbers

SCHEME

(define y (list '+ 237))
y
(eval y)

SCHEME

*

SCHEME

(* 2 3 7)

SCHEME

(apply * (list 2 3 7))

Challenge 3: Can you do it?

check if + is a procedure. Use + to add three numbers in a list.

SCHEME

+
(apply + (list 2 3 7))

Key Points

  • Scheme is a programming language that uses lists
  • Scheme can be used to create very powerful compact programs
  • Scheme has specifications which if implemented can be imported to give additional functionality such as formatted output
  • Symbols can be used to create s-expressions that allow one to define and evaluate in separate steps