SICP Collections
1 Introduction
2 Installation
3 Introduction to the #lang sicp language
4 Introduction to the SICP Picture Language
5 Reference
6 Example
7 Vectors
make-vect
vector-xcor
vector-ycor
vector-add
vector-sub
vector-scale
8 Frames
make-frame
frame-origin
frame-edge1
frame-edge2
make-relative-frame
frame-coord-map
9 Segments
make-segment
segment-start
segment-end
10 Primitive Painters
number->painter
segments->painter
procedure->painter
picture->painter
load-painter
11 Higher Order Painters
transform-painter
flip-horiz
flip-vert
rotate90
rotate180
rotate270
beside
below
superpose
12 Simple Builtin Painters
13 Painting
paint
paint-hi-res
14 Authors
15 Other
Index
6.12

SICP Collections

 (require sicp) package: sicp
 (require sicp-pict) package: sicp

1 Introduction

This package contains two collections.

The sicp collection contains a #lang sicp language ideal for studying the book "Structure and Interpretation of Computer Programs" by Gerald Jay Sussman and Hal Abelson. The book is usually referred to simply as SICP.

The second sicp-pict collection contains the picture language used in SICP. The non-standard primitives cons-stream and amb are also provided.

2 Installation

Use DrRacket to install the sicp package like this:

  1. Open the Package Manager: in DrRacket choose the menu "File" then choose "Package Manager...".

  2. In the tab "Do What I Mean" find the text field and enter: "sicp"

  3. Finally click the "Install" button.

  4. Test it. Make sure DrRacket has "Determine language from source" in the bottom left corner. Write the following program and click run:

    #lang sicp

    (inc 42)

    The expected output is 43.

3 Introduction to the #lang sicp language

The programs in the book are written in (a subset of) the programming language Scheme. As the years have passed the programming language Scheme has evolved. The language #lang sicp provides you with a version of R5RS (the fifth revision of Scheme) changed slightly in order for programs in SICP to run as is.

The changes are as follows:

The identifers true, false, and, nil are provided with values #t, #f, and, '() respectively.

The following functions of one variable are provided:

(define (identity x) x)         the identity funciton

(define (inc x) (+ x 1))        increment

(define (dec x) (- x 1))        decrement

There are no streams in R5RS, so the sicp language provides the primitives cons-stream and stream-null? that respectively constructs a stream and tests whether a stream is the null stream. The null-stream is provided as the-empty-stream.

Finally the function runtime is provided. It gives you the current time measured as the number of seconds passed since a fixed beginning.

To use the sicp language simply use #lang sicp as the first line of your program. If you need to use Racket libraries, then use #%require (Note: R5RS has no require so in order not to break any programs using the name require to other things, the name #%require is used instead.

4 Introduction to the SICP Picture Language

The SICP Picture Language is a small language for drawing pictures. It shows the power of data abstraction and closure. The picture language stems from Peter Henderson’s 1982 paper "Functional Geometry" and was included by Hal Abelson in "Structure and Interpretation of Computer Programs".

Before using this package, read section 2.2.4 of SICP, which is an excellent introduction to the ideas of the picture language. This manual is meant as a reference guide.

Peter Henderson has written an updated version of "Functional Geometry", which explains how to construct the Escher fish image.

Note: The primitives cons-stream and amb needed in other chapters of SICP are also provided.

5 Reference

The basic concept of the picture language is a painter. A painter draws it’s image (shifted and scaled) within a frame given by a parallelogram. Painters can be combined to construct new painters.

6 Example

Using sicp-pict from a "#lang sicp" program:

#lang sicp

(#%require sicp-pict)

(paint einstein)

Using sicp-pict from a "#lang racket" program:

#lang racket

(require sicp-pict)

(paint einstein)

From the REPL:

> (require sicp-pict)
> (paint (number->painter 0))
> (paint diagonal-shading)
> (paint-hires  (below (beside diagonal-shading
                        (rotate90 diagonal-shading))
                (beside (rotate270 diagonal-shading)
                        (rotate180 diagonal-shading))))
> (paint einstein)

7 Vectors

A mathematical vector is called a vect here, in order to avoid confusion with the builtin vectors of Scheme.

procedure

(make-vect x y)  vect?

  x : number?
  y : number?
Constructs a vect with the given coordinates.

procedure

(vector-xcor v)  number?

  v : vect?
Returns the x-coordinate.

procedure

(vector-ycor v)  number?

  v : vect?
Returns the y-coordinate.

procedure

(vector-add v w)  vect?

  v : vect?
  w : vect?
Adds the two vects by adding their coordinates pairwise.

procedure

(vector-sub v w)  vect?

  v : vect?
  w : vect?
Subtracts the two vects by subtracting their coordinates pairwise.

procedure

(vector-scale s v)  vect?

  s : number?
  v : vect?
Scales the vect by multiplying each coordinate of "v" with the number "s".

8 Frames

A frame is descibed by three vectors.

    ^

    | frame edge2 vector

    |

   _|__________>

   /|         frame edge1 vector

  /

 /

/ frame origin pointer

procedure

(make-frame origin edge1 edge2)  frame?

  origin : vect?
  edge1 : vect?
  edge2 : vect
Constructs a frame from a frame origin vector and two frame edge vectors.

procedure

(frame-origin f)  vect?

  f : frame?

procedure

(frame-edge1 f)  vect?

  f : frame?

procedure

(frame-edge2 f)  vect?

  f : frame?
Extracts the origin, first edge or second edge from a frame.

procedure

(make-relative-frame origin corner1 corner2)  (frame? -> frame?)

  origin : vect?
  corner1 : vect?
  corner2 : vect?
The function make-relative-frame provides a convenient way to transform frames. Given a frame and three points : origin, corner1, and corner2 (expressed in frame coordinates), it returns a new frame with those corners.

procedure

(frame-coord-map f)  (vect? -> vect?)

  f : frame?
Each frame determines a system of "frame coordinates" (x,y) where (0,0) is the origin of the frame, x represents the displacement along the first edge (as a fraction of the length of the edge) and y is the displacement along the second edge.

The frame coordinate map is returned by frame-coord-map. E.g. these expression return the same value:

((frame-coord-map a-frame) (make-vect 0 0))

(frame-origin a-frame)

9 Segments

A pair of vectors determines a directed line segment - the segment running from the endpoint of the first vector to the endpoint of the second vector.

procedure

(make-segment from to)  segment?

  from : vect?
  to : vect?

procedure

(segment-start s)  vect?

  s : segment?

procedure

(segment-end s)  vect?

  s : segment?

10 Primitive Painters

Painters take a frame and draw an image, transformed to fit inside the frame.

There are four ways to create painters:

procedure

(number->painter color)  painter?

  color : 0..255
Constructs a painter that fills the frame with a gray color indicated by the number. 0 is black and 255 is white.

procedure

(segments->painter los)  painter?

  los : list-of-segment?
Constructs a painter that draws a stick figure given by the segments (wrt the unit square).

procedure

(procedure->painter p)  painter?

  p : procedure?
Creates painters from procedures. We assume that the procedure f is defined on the unit square.

Then to plot a point p in the target frame, we find the inverse image T^-1(p) of p under the transformation that maps the unit square to the target, and find the value of f at T-1(p).

procedure

(picture->painter p)  painter?

  p : picture
The picture p is defined on some frame.

Given a point p in the target frame, we compute T^-1(p) where T is the transformation that takes the picture frame to the target frame, and find the picture value at the closest integer point.

procedure

(load-painter filename)  painter?

  filename : path?
Uses the image file given by filename to create a painter.

11 Higher Order Painters

procedure

(transform-painter origin corner1 corner2)

  (painter? -> painter?)
  origin : vect?
  corner1 : vect?
  corner2 : vect?
A painter can be transformed to produce a new painter which, when given a frame, calls the original painter on the transformed frame.

Transform-painter will given an origin and two corners, return a function that takes a painter as argument and returns a transformed painter.

procedure

(flip-horiz p)  painter?

  p : painter
Returns a painter that flips the image horizontally.

procedure

(flip-vert p)  painter?

  p : painter
Returns a painter that flips the image vertically.

procedure

(rotate90 p)  painter?

  p : painter

procedure

(rotate180 p)  painter?

  p : painter

procedure

(rotate270 p)  painter?

  p : painter
Returns a painter that rotates the image.

procedure

(beside p1 p2)  painter?

  p1 : painter
  p2 : painter
Constructs a painter that paints the images side-by-side.

procedure

(below p1 p2)  painter?

  p1 : painter
  p2 : painter
Constructs a painter that paints the second image below the first.

procedure

(superpose p1 p2)  painter?

  p1 : painter
  p2 : painter
Constructs a painter that paints the two images on top of each other.

12 Simple Builtin Painters

The following painter values are buitin:

black, white and gray Fills the frame with black (0), white (255) or gray (150).

diagonal-shading Fills the frame with a shades of gray. The color transition goes from black in the upper left corner is black, to gray in the bottom right corner.

einstein Draws an image of Einstein.

13 Painting

The procedures paint and paint-hi-res takes a painter as input and return a snip containing the painter’s image. A snip is an image that DrScheme can display automatically.

procedure

(paint p)  snip?

  p : painter?

procedure

(paint-hi-res p)  snip?

  p : painter?

14 Authors

Abelson & Sussman: Structure and Interpretation of Computer Programs.

Daniel Coore: Original MIT Scheme code.

Mike Sperber: PLT port.

Jens Axel Søgaard: Documentation.

Javier Olaechea: Fixed amb.

Neil Van Dyke: The SICP language. Maintained the sicp package for years.

15 Other

See also the readme.html from the SICP web-site for more documentation and exercises.

Peter Henderson’s "Functional Geometry".

Index

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

 

Authors
below
beside
Escher
Example
flip-horiz
flip-vert
frame-coord-map
frame-edge1
frame-edge2
frame-origin
Frames
geometry
Higher Order Painters
Installation
Introduction
Introduction to the #lang sicp language
Introduction to the SICP Picture Language
load-painter
make-frame
make-relative-frame
make-segment
make-vect
number->painter
Other
paint
paint-hi-res
painter
Painting
picture
picture->painter
Primitive Painters
procedure->painter
Reference
rotate180
rotate270
rotate90
segment-end
segment-start
Segments
segments->painter
sicp
SICP
sicp
SICP Collections
sicp-pict
Simple Builtin Painters
superpose
transform-painter
vector-add
vector-scale
vector-sub
vector-xcor
vector-ycor
Vectors