Programing Portfolio => Scheme Examples
I have experience working in Scheme, which is a variation of Lisp. Scheme code is not compiled into an executable. Instead, an interpreter is used, allowing line-by-line interaction with the program. Below are some examples of my work.
The vinter's problem is simply a conversion of volume. Given a vat of a certain height and radius, how many bottles of a given volume can be filled?
program code:;;; Katherine Irvine
;;; Procedure for computing the vintner's problem
;;; Inputs:
;;; radius is the radius of the vat (in metres)
;;; height is the height of the vat (in metres)
;;; bottle is the volume of the bottles (in litres)
;;; Outputs:
;;; the number of bottles
(define Pi 3.141592653589793) ; defines the value of pi
(define area-circle (lambda (r) (* Pi r r))) ; gives the area in m2
(define volume-cylinder (lambda (r h) (* (area-circle r) h))) ; gives the volume in m3
(define m3toL (lambda (vol) (* vol 1000))) ; converts m3 to litres
(define vats2bottles (lambda (radius height litres) ; solves the problem.
(ceiling (/ (m3toL (volume-cylinder radius height)) litres))))
interactions:
Welcome to DrScheme, version 200. Language: Advanced Student. Teachpack: /cs/local/lib/pkg/drscheme-200/collects/ubc/subst.ss. >(vats2bottles 5 10 7) 112200 >(vats2bottles 5 10 4) 196350 >(vats2bottles 5 10 3) 261800 >(vats2bottles 4 12 7) 86170 >(vats2bottles 4 12 4) 150797 >(vats2bottles 4 12 3) 201062 >[top]
A tree is one way of showing relationships graphically, with nodes (objects) being shown as dots, and edges (relationships) being shown as lines between two of the nodes. The "k-ary" part describes the complexity of the tree (which is different from it's size). For example, binary trees, like the one shown here, have two edges coming down from each node.

The code below includes one method of calculating the number of nodes at a given level of a tree, and several methods of calculating the total number of nodes in a tree of a given size.
;;; Katherine Irvine
;;;
;;; program to compute the number of nodes on a certain level of a k-ary tree
;;; Inputs:
;;; n : the level of the tree
;;; k : the complexity of the tree
;;; Output:
;;; the number of nodes at that level
(define number-at-level
(lambda (n k)
(if (= n 0)
1
(* k (number-at-level (sub1 n) k)))))
;;; program to compute the number of nodes in a k-ary tree, down to a certain level
;;; Inputs:
;;; n : the level of the tree
;;; k : the complexity of the tree
;;; Output:
;;; the number of nodes in the tree
(define total-number
(lambda (n k)
(if (= n 0)
1
(+ (number-at-level n k) (total-number (sub1 n) k)))))
;;; program to compute the number of nodes in a k-ary tree, down to a certain level,
;;; using the helper program total-up-helper
;;; Inputs:
;;; n : the level of the tree
;;; k : the complexity of the tree
;;; Output:
;;; the number of nodes in the tree
(define total-up
(lambda (n k)
(total-up-helper n k 0 1 0)))
;;; program to compute the number of nodes in a k-ary tree, down to a certain level,
;;; using tail-recursion
;;; Inputs:
;;; n : the bottom level of the tree
;;; k : the complexity of the tree
;;; i : the current level of the tree
;;; size : the number of nodes in the current level
;;; total : the total number of nodes in all previous levels
;;; Output:
;;; the number of nodes in the tree
(define total-up-helper
(lambda (n k i size total)
(if (= n i)
(+ total size)
(total-up-helper n k (add1 i) (number-at-level (add1 i) k) (+ size total)))))
[top]Scheme can also work with drawing objects. Here is a sample of code used to draw, scale and translate various shapes.
program code:(define square
(lambda (mv dr)
(begin
(mv 0 0)
(dr 0 1)
(dr 1 1)
(dr 1 0)
(dr 0 0))))
(define scale
(lambda (p s)
(lambda (mv dr)
(p
(lambda (x y) (mv (* s x) (* s y)))
(lambda (x y) (dr (* s x) (* s y)))))))
(define translate
(lambda (p x1 y1)
(lambda (mv dr)
(p
(lambda (x y) (mv (+ x1 x) (+ y1 y)))
(lambda (x y) (dr (+ x1 x) (+ y1 y)))))))
(define show
(lambda (p)
(p draw-move draw-line)))
;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; adds a title and other info to the graphics picture
(define add-title
(lambda (description)
(begin
(draw-move 25 475)
(draw-display "Katherine Irvine")
(draw-move 25 465)
(draw-display 38933024)
(draw-move 25 455)
(draw-display "Lab L1A")
(draw-move 25 445)
(draw-display description))))
;;; draws a 25 X 25 square from the origin
(define 25x25
(lambda ()
(show (scale square 25))))
;;; draws a 25 x 25 square from (100,100)
(define 25at100
(lambda ()
(show (translate (scale square 25)100 100))))
;;; scales then translates a square
(define scale-and-translate
(lambda ()
(show (translate (scale square 10) 40 40))))
;;; translates then scales a square
(define translate-and-scale
(lambda ()
(show (scale (translate square 40 40) 10))))
;;; draws an equilateral triangle (1 unit is 10 pixels)
(define equilateral-triangle
(lambda (mv dr)
(begin
(mv 0 0)
(dr 1 0)
(dr .5 .866)
(dr 0 0))))
;;; draws a house
(define draw-house
(lambda (mv dr)
(begin
(square mv dr)
((translate equilateral-triangle 0 1) mv dr))))
;;; scales any shape by 25 and translates it so the lower-left corner is at (100,100)
(define general-25at100
(lambda (p)
(translate (scale p 25)100 100)))
;;; draws a house scaled by 25 and starting at 100, 100
(define big-house
(lambda ()
(show (general-25at100 draw-house))))
;;; draws a cross (+)
(define cross
(lambda (mv dr)
(begin
(mv 0 1)
(dr 2 1)
(mv 1 0)
(dr 1 2))))
[top]