1 Example programs
2 The environment
3 World programming and Images API
big-bang
on-tick
on-key
key=?
to-draw
stop-when
4 Basic operations
5 Keyboard shortcuts
6 Acknowledgements
5.3.2

WeScheme

WeScheme is an web-based programming environment that allows us to write, run, and share programs on the web. Programs written in WeScheme should be available from any computer with a capable Javascript-enabled web browser. The editing environment, the compiler, and the associated runtime libraries are all hosted on WeScheme, eliminating installation hassles. WeScheme allows us to easily share programs by creating share URLs; these share URLs can be used to run a program or, if the author permits it, allow anyone to view the source to that program.

Web programs are typically interactive, so WeScheme provides special support for World programs that can interact with timer ticks, keyboard events, and images.

1 Example programs

Here are a few example programs that can give an idea of the kinds of things you can do in WeScheme. You can:

2 The environment

Let’s jump in and explore WeScheme by running a few programs.

Open up a web browser to http://www.wescheme.org. Press the Start Coding button. The following editor page should be divided into a top definitions section, and a bottom interactions section. Click onto the definitions top half of the window and enter in the following text, quotes and all:

"hello world"

Next, press the Run button at the toolbar at the top. If all goes well, we should see a "hello world" appear on the bottom window.

Next, add another line in the definitions window:

(bitmap/url "http://racket-lang.org/logo.png")

Press the Run button again. We should now see an image in the Interactions window as well.

Web images are values, as are strings, numbers, booleans, and structures. You can even apply algebra on them. Try:

(rotate 45 (bitmap/url "http://racket-lang.org/logo.png"))

or
(overlay (bitmap/url "http://racket-lang.org/logo.png")
         (bitmap/url "http://www.wescheme.org/css/images/BigLogo.png"))
for example. Many more image functions are built-into WeScheme; you can explore the functions in World programming and Images API.

3 World programming and Images API

procedure

(big-bang w h ...)  world

  w : world
  h : big-bang-handler
Start a big bang computation. The big-bang consumes an initial world, as well as several handlers to configure it, described in this section:

procedure

(on-tick tick-f delay)  big-bang-handler

  tick-f : ([w world] -> world)
  delay : real
(on-tick tick-f)  big-bang-handler
  tick-f : ([w world] -> world)
Tells big-bang to update the world during clock ticks.

By default, this will send a clock tick 28 times a second, but if given delay, it will use that instead.
;; The world is a number
;; tick: world -> world
(define (tick world)
  (add1 world))
 
(big-bang 0
          (on-tick tick 2)) ;; tick every two seconds

procedure

(on-key key-f)  big-bang-handler

  key-f : ([w world] [k key] -> world)
Tells big-bang to update the world when a key is pressed. The key-f function will be called with the particular key being pressed.

;; The world is a number.
 
;; handle-key: world key -> image
(define (handle-key w k)
  (cond [(key=? k "up")
         (add1 w)]
        [(key=? k "down")
         (sub1 w)]
        [else
         w]))
(big-bang 0
          (on-key handle-key))

procedure

(key=? a-key a-string)  boolean

  a-key : key
  a-string : string
Returns true if a-key is equal to a-string.

procedure

(to-draw draw-f)  big-bang-handler

  draw-f : ([w world] -> image)
Tells big-bang how to update what the world looks like. The draw function will be called every time an event occurs.

;; The world is a number.
 
;; draw: world -> image
(define (draw world)
  (circle world "solid" "blue"))
 
(big-bang 20
          (to-draw draw))

To support some legacy WeScheme applications, the name on-redraw is an alias for to-draw.

procedure

(stop-when stop?)  big-bang-handler

  stop? : ([w world] ->  boolean)
Tells big-bang when to stop.
;; the world is a number
 
;; stop?: world -> boolean
(define (stop? world)
  (> world 10))
 
(big-bang 0
          (stop-when stop?)
          (on-tick add1 1))

Here is a listing of the functions you can use to make images.

struct

(struct   color (red green blue alpha)
    #:extra-constructor-name make-color)
  red : (and/c natural-number/c (<=/c 255))
  green : (and/c natural-number/c (<=/c 255))
  blue : (and/c natural-number/c (<=/c 255))
  alpha : (and/c natural-number/c (<=/c 255))
The color struct defines a color with red, green, blue, and alpha components that range from 0 to 255.

The red, green, and blue fields combine to make a color, with the higher values meaning more of the given color. For example, (make-color 255 0 0) makes a bright red color and (make-color 255 0 255) makes a bright purple.

The alpha field controls the transparency of the color. A value of 255 means that the color is opaque and 0 means the color is fully transparent.

The constructor, make-color, also accepts only three arguments, in which case the three arguments are used for the red, green, and blue fields, and the alpha field defaults to 255.

procedure

(empty-scene width height)  image?

  width : (and/c real? (not/c negative?))   height : (and/c real? (not/c negative?)) (empty-scene width height color)  image?   width : (and/c real? (not/c negative?))   height : (and/c real? (not/c negative?))   color : image-color?
Creates an empty scene, i.e., a white rectangle with a black outline.

Example:

> (empty-scene 160 90)

image

The three-argument version creates a rectangle of the specified color with a black outline.

procedure

(scene+line image x1 y1 x2 y2 color)  image?

  image : image?   x1 : real?   y1 : real?   x2 : real?   y2 : real?   color : image-color?
Adds a line to the image scene, starting from the point (x1,y1) and going to the point (x2,y2); unlike add-line, this function crops the resulting image to the size of scene.

Examples:

> (scene+line (ellipse 40 40 "outline" "maroon")
              0 40 40 0 "maroon")

image

> (scene+line (rectangle 40 40 "solid" "gray")
              -10 50 50 -10 "maroon")

image

procedure

(place-image image x y scene)  image?

  image : image?   x : real?   y : real?   scene : image?
Places image onto scene with its center at the coordinates (x,y) and crops the resulting image so that it has the same size as scene. The coordinates are relative to the top-left of scene.

procedure

(place-image/align   image        
    x        
    y        
    x-place        
    y-place        
    scene)     image?
  image : image?
  x : real?
  y : real?
  x-place : x-place?
  y-place : y-place?
  scene : image?
Like place-image, but uses image’s x-place and y-place to anchor the image. Also, like place-image, place-image/align crops the resulting image so that it has the same size as scene.

Examples:

> (place-image/align (triangle 48 "solid" "yellowgreen")
                     64 64 "right" "bottom"
                     (rectangle 64 64 "solid" "mediumgoldenrod"))

image

> (beside
   (place-image/align (circle 8 "solid" "tomato")
                      0 0 "center" "center"
                      (rectangle 32 32 "outline" "black"))
   (place-image/align (circle 8 "solid" "tomato")
                      8 8 "center" "center"
                      (rectangle 32 32 "outline" "black"))
   (place-image/align (circle 8 "solid" "tomato")
                      16 16 "center" "center"
                      (rectangle 32 32 "outline" "black"))
   (place-image/align (circle 8 "solid" "tomato")
                      24 24 "center" "center"
                      (rectangle 32 32 "outline" "black"))
   (place-image/align (circle 8 "solid" "tomato")
                      32 32 "center" "center"
                      (rectangle 32 32 "outline" "black")))

image

procedure

(circle radius mode color)  image?

  radius : (and/c real? (not/c negative?))   mode : mode?   color : image-color? (circle radius outline-mode color)  image?   radius : (and/c real? (not/c negative?))   outline-mode : (or/c 'outline "outline")   color : image-color?
Constructs a circle with the given radius, height, mode, and color.

Examples:

> (circle 30 "outline" "red")

image

> (circle 20 "solid" "blue")

image

> (circle 20 100 "blue")

image

procedure

(star side-length mode color)  image?

  side-length : (and/c real? (not/c negative?))   mode : mode?   color : image-color? (star side-length outline-mode color)  image?   side-length : (and/c real? (not/c negative?))   outline-mode : (or/c 'outline "outline")   color : image-color?
Constructs a star with five points. The side-length argument determines the side length of the enclosing pentagon.

Example:

> (star 40 "solid" "gray")

image

procedure

(radial-star   point-count        
    inner-radius        
    outer-radius        
    mode        
    color)     image?
  point-count : (and/c integer? (>=/c 2))
  inner-radius : (and/c real? (not/c negative?))
  outer-radius : (and/c real? (not/c negative?))
  mode : mode?
  color : image-color?
(radial-star   point-count        
    inner-radius        
    outer-radius        
    outline-mode        
    color)     image?
  point-count : (and/c integer? (>=/c 2))
  inner-radius : (and/c real? (not/c negative?))
  outer-radius : (and/c real? (not/c negative?))
  outline-mode : (or/c 'outline "outline")
  color : image-color?
Constructs a star-like polygon where the star is specified by two radii and a number of points. The first radius determines where the points begin, the second determines where they end, and the point-count argument determines how many points the star has.

Examples:

> (radial-star 8 8 64 "solid" "darkslategray")

image

> (radial-star 32 30 40 "outline" "black")

image

procedure

(star-polygon   side-length        
    side-count        
    step-count        
    mode        
    color)     image?
  side-length : (and/c real? (not/c negative?))
  side-count : side-count?
  step-count : step-count?
  mode : mode?
  color : image-color?
(star-polygon   side-length        
    side-count        
    step-count        
    outline-mode        
    color)     image?
  side-length : (and/c real? (not/c negative?))
  side-count : side-count?
  step-count : step-count?
  outline-mode : (or/c 'outline "outline")
  color : image-color?
Constructs an arbitrary regular star polygon (a generalization of the regular polygons). The polygon is enclosed by a regular polygon with side-count sides each side-length long. The polygon is actually constructed by going from vertex to vertex around the regular polgon, but connecting every step-count-th vertex (i.e., skipping every (- step-count 1) verticies).

For example, if side-count is 5 and step-count is 2, then this function produces a shape just like star.

Examples:

> (star-polygon 40 5 2 "solid" "seagreen")

image

> (star-polygon 40 7 3 "outline" "darkred")

image

> (star-polygon 20 10 3 "solid" "cornflowerblue")

image

procedure

(overlay i1 i2 is ...)  image?

  i1 : image?   i2 : image?   is : image?
Overlays all of its arguments building a single image. The first argument goes on top of the second argument, which goes on top of the third argument, etc. The images are all lined up on their centers.

Examples:

> (overlay (rectangle 30 60 "solid" "orange")
           (ellipse 60 30 "solid" "purple"))

image

> (overlay (ellipse 10 10 "solid" "red")
           (ellipse 20 20 "solid" "black")
           (ellipse 30 30 "solid" "red")
           (ellipse 40 40 "solid" "black")
           (ellipse 50 50 "solid" "red")
           (ellipse 60 60 "solid" "black"))

image

> (overlay (regular-polygon 20 5 "solid" (make-color  50  50 255))
           (regular-polygon 26 5 "solid" (make-color 100 100 255))
           (regular-polygon 32 5 "solid" (make-color 150 150 255))
           (regular-polygon 38 5 "solid" (make-color 200 200 255))
           (regular-polygon 44 5 "solid" (make-color 250 250 255)))

image

procedure

(polygon vertices mode color)  image?

  vertices : (listof real-valued-posn?)   mode : mode?   color : image-color? (polygon vertices outline-mode color)  image?   vertices : (listof real-valued-posn?)   outline-mode : (or/c 'outline "outline")   color : image-color?
Constructs a polygon connecting the given vertices.

Examples:

> (polygon (list (make-posn 0 0)
                 (make-posn -10 20)
                 (make-posn 60 0)
                 (make-posn -10 -20))
           "solid"
           "burlywood")

image

> (polygon (list (make-posn 0 0)
                 (make-posn 0 40)
                 (make-posn 20 40)
                 (make-posn 20 60)
                 (make-posn 40 60)
                 (make-posn 40 20)
                 (make-posn 20 20)
                 (make-posn 20 0))
           "solid"
           "plum")

image

procedure

(rectangle width height mode color)  image?

  width : real?   height : real?   mode : mode?   color : image-color?
(rectangle   width        
    height        
    outline-mode        
    color)     image?
  width : real?   height : real?   outline-mode : (or/c 'outline "outline")   color : image-color?
Constructs a rectangle with the given width, height, mode, and color.

Examples:

> (rectangle 40 20 "outline" "black")

image

> (rectangle 20 40 "solid" "blue")

image

procedure

(regular-polygon   side-length        
    side-count        
    mode        
    color)     image?
  side-length : (and/c real? (not/c negative?))
  side-count : side-count?
  mode : mode?
  color : image-color?
(regular-polygon   side-length        
    side-count        
    outline-mode        
    color)     image?
  side-length : (and/c real? (not/c negative?))
  side-count : side-count?
  outline-mode : (or/c 'outline "outline")
  color : image-color?
Constructs a regular polygon with side-count sides.

Examples:

> (regular-polygon 50 3 "outline" "red")

image

> (regular-polygon 40 4 "outline" "blue")

image

> (regular-polygon 20 8 "solid" "red")

image

procedure

(rhombus side-length angle mode color)  image?

  side-length : (and/c real? (not/c negative?))   angle : angle?   mode : mode?   color : image-color?
(rhombus   side-length        
    angle        
    outline-mode        
    color)     image?
  side-length : (and/c real? (not/c negative?))   angle : angle?   outline-mode : (or/c 'outline "outline")   color : image-color?
Constructs a four sided polygon with all equal sides and thus where opposite angles are equal to each other. The top and bottom pair of angles is angle and the left and right are (- 180 angle).

Examples:

> (rhombus 40 45 "solid" "magenta")

image

> (rhombus 80 150 "solid" "mediumpurple")

image

procedure

(square side-len mode color)  image?

  side-len : (and/c real? (not/c negative?))   mode : mode?   color : image-color? (square side-len outline-mode color)  image?   side-len : (and/c real? (not/c negative?))   outline-mode : (or/c 'outline "outline")   color : image-color?
Constructs a square.

Examples:

> (square 40 "solid" "slateblue")

image

> (square 50 "outline" "darkmagenta")

image

procedure

(triangle side-length mode color)  image?

  side-length : (and/c real? (not/c negative?))   mode : mode?   color : image-color?
(triangle   side-length        
    outline-mode        
    color)     image?
  side-length : (and/c real? (not/c negative?))   outline-mode : (or/c 'outline "outline")   color : image-color?
Constructs a upward-pointing equilateral triangle. The side-length argument determines the length of the side of the triangle.

Example:

> (triangle 40 "solid" "tan")

image

procedure

(right-triangle   side-length1        
    side-length2        
    mode        
    color)     image?
  side-length1 : (and/c real? (not/c negative?))
  side-length2 : (and/c real? (not/c negative?))
  mode : mode?
  color : image-color?
(right-triangle   side-length1        
    side-length2        
    outline-mode        
    color)     image?
  side-length1 : (and/c real? (not/c negative?))
  side-length2 : (and/c real? (not/c negative?))
  outline-mode : (or/c 'outline "outline")
  color : image-color?
Constructs a triangle with a right angle where the two sides adjacent to the right angle have lengths side-length1 and side-length2.

Example:

> (right-triangle 36 48 "solid" "black")

image

procedure

(isosceles-triangle   side-length        
    angle        
    mode        
    color)     image?
  side-length : (and/c real? (not/c negative?))
  angle : angle?
  mode : mode?
  color : image-color?
(isosceles-triangle   side-length        
    angle        
    outline-mode        
    color)     image?
  side-length : (and/c real? (not/c negative?))
  angle : angle?
  outline-mode : (or/c 'outline "outline")
  color : image-color?
Creates a triangle with two equal-length sides, of length side-length where the angle between those sides is angle. The third leg is straight, horizontally. If the angle is less than 180, then the triangle will point up and if the angle is more, then the triangle will point down.

Examples:

> (isosceles-triangle 200 170 "solid" "seagreen")

image

> (isosceles-triangle 60 30 "solid" "aquamarine")

image

> (isosceles-triangle 60 330 "solid" "lightseagreen")

image

To create a triangle given known sides and angles, the following family of functions are useful:

They all construct a triangle oriented as follows:

procedure

(ellipse width height mode color)  image?

  width : (and/c real? (not/c negative?))   height : (and/c real? (not/c negative?))   mode : mode?   color : image-color?
Constructs an ellipse with the given width, height, mode, and color.

Examples:

> (ellipse 60 30 "outline" "black")

image

> (ellipse 30 60 "solid" "blue")

image

> (ellipse 30 60 100 "blue")

image

procedure

(line x1 y1 color)  image?

  x1 : real?   y1 : real?   color : image-color?
Constructs an image representing a line segment that connects the points (0,0) to (x1,y1).

Examples:

> (line 30 30 "black")

image

> (line -30 20 "red")

image

> (line 30 -20 "red")

image

procedure

(add-line image x1 y1 x2 y2 color)  image?

  image : image?   x1 : real?   y1 : real?   x2 : real?   y2 : real?   color : image-color?
Adds a line to the image image, starting from the point (x1,y1) and going to the point (x2,y2). Unlike scene+line, if the line passes outside of image, the image gets larger to accommodate the line.

Examples:

> (add-line (ellipse 40 40 "outline" "maroon")
            0 40 40 0 "maroon")

image

> (add-line (rectangle 40 40 "solid" "gray")
            -10 50 50 -10 "maroon")

image

procedure

(overlay i1 i2 is ...)  image?

  i1 : image?   i2 : image?   is : image?
Overlays all of its arguments building a single image. The first argument goes on top of the second argument, which goes on top of the third argument, etc. The images are all lined up on their centers.

Examples:

> (overlay (rectangle 30 60 "solid" "orange")
           (ellipse 60 30 "solid" "purple"))

image

> (overlay (ellipse 10 10 "solid" "red")
           (ellipse 20 20 "solid" "black")
           (ellipse 30 30 "solid" "red")
           (ellipse 40 40 "solid" "black")
           (ellipse 50 50 "solid" "red")
           (ellipse 60 60 "solid" "black"))

image

> (overlay (regular-polygon 20 5 "solid" (make-color  50  50 255))
           (regular-polygon 26 5 "solid" (make-color 100 100 255))
           (regular-polygon 32 5 "solid" (make-color 150 150 255))
           (regular-polygon 38 5 "solid" (make-color 200 200 255))
           (regular-polygon 44 5 "solid" (make-color 250 250 255)))

image

procedure

(overlay/xy i1 x y i2)  image?

  i1 : image?   x : real?   y : real?   i2 : image?
Constructs an image by overlaying i1 on top of i2. The images are initially lined up on their upper-left corners and then i2 is shifted to the right by x pixels to and down by y pixels.

This is the same as (underlay/xy i2 (- x) (- y) i1).

See also overlay/offset and underlay/offset.

Examples:

> (overlay/xy (rectangle 20 20 "outline" "black")
              20 0
              (rectangle 20 20 "outline" "black"))

image

> (overlay/xy (rectangle 20 20 "solid" "red")
              10 10
              (rectangle 20 20 "solid" "black"))

image

> (overlay/xy (rectangle 20 20 "solid" "red")
              -10 -10
              (rectangle 20 20 "solid" "black"))

image

> (overlay/xy
   (overlay/xy (ellipse 40 40 "outline" "black")
               10
               15
               (ellipse 10 10 "solid" "forestgreen"))
   20
   15
   (ellipse 10 10 "solid" "forestgreen"))

image

procedure

(overlay/align x-place y-place i1 i2 is ...)  image?

  x-place : x-place?   y-place : y-place?   i1 : image?   i2 : image?   is : image?
Overlays all of its image arguments, much like the overlay function, but using x-place and y-place to determine where the images are lined up. For example, if x-place and y-place are both "middle", then the images are lined up on their centers.

Examples:

> (overlay/align "left" "middle"
                 (rectangle 30 60 "solid" "orange")
                 (ellipse 60 30 "solid" "purple"))

image

> (overlay/align "right" "bottom"
                 (rectangle 20 20 "solid" "silver")
                 (rectangle 30 30 "solid" "seagreen")
                 (rectangle 40 40 "solid" "silver")
                 (rectangle 50 50 "solid" "seagreen"))

image

procedure

(underlay i1 i2 is ...)  image?

  i1 : image?   i2 : image?   is : image?
Underlays all of its arguments building a single image.

It behaves like overlay, but with the arguments in the reverse order. That is, the first argument goes underneath of the second argument, which goes underneath the third argument, etc. The images are all lined up on their centers.

Examples:

> (underlay (rectangle 30 60 "solid" "orange")
            (ellipse 60 30 "solid" "purple"))

image

> (underlay (ellipse 10 60 "solid" "red")
            (ellipse 20 50 "solid" "black")
            (ellipse 30 40 "solid" "red")
            (ellipse 40 30 "solid" "black")
            (ellipse 50 20 "solid" "red")
            (ellipse 60 10 "solid" "black"))

image

> (underlay (ellipse 10 60 40 "red")
            (ellipse 20 50 40 "red")
            (ellipse 30 40 40 "red")
            (ellipse 40 30 40 "red")
            (ellipse 50 20 40 "red")
            (ellipse 60 10 40 "red"))

image

procedure

(underlay/xy i1 x y i2)  image?

  i1 : image?   x : real?   y : real?   i2 : image?
Constructs an image by underlaying i1 underneath i2. The images are initially lined up on their upper-left corners and then i2 is shifted to the right by x pixels to and down by y pixels.

This is the same as (overlay/xy i2 (- x) (- y) i1).

See also underlay/offset and overlay/offset.

Examples:

> (underlay/xy (rectangle 20 20 "outline" "black")
               20 0
               (rectangle 20 20 "outline" "black"))

image

> (underlay/xy (rectangle 20 20 "solid" "red")
               10 10
               (rectangle 20 20 "solid" "black"))

image

> (underlay/xy (rectangle 20 20 "solid" "red")
               -10 -10
               (rectangle 20 20 "solid" "black"))

image

> (underlay/xy
   (underlay/xy (ellipse 40 40 "solid" "gray")
                10
                15
                (ellipse 10 10 "solid" "forestgreen"))
   20
   15
   (ellipse 10 10 "solid" "forestgreen"))

image

procedure

(underlay/align x-place y-place i1 i2 is ...)  image?

  x-place : x-place?   y-place : y-place?   i1 : image?   i2 : image?   is : image?
Underlays all of its image arguments, much like the underlay function, but using x-place and y-place to determine where the images are lined up. For example, if x-place and y-place are both "middle", then the images are lined up on their centers.

Examples:

> (underlay/align "left" "middle"
                  (rectangle 30 60 "solid" "orange")
                  (ellipse 60 30 "solid" "purple"))

image

> (underlay/align "right" "top"
                  (rectangle 50 50 "solid" "seagreen")
                  (rectangle 40 40 "solid" "silver")
                  (rectangle 30 30 "solid" "seagreen")
                  (rectangle 20 20 "solid" "silver"))

image

> (underlay/align "left" "middle"
                  (rectangle 50 50 50 "seagreen")
                  (rectangle 40 40 50 "seagreen")
                  (rectangle 30 30 50 "seagreen")
                  (rectangle 20 20 50 "seagreen"))

image

procedure

(beside i1 i2 is ...)  image?

  i1 : image?   i2 : image?   is : image?
Constructs an image by placing all of the argument images in a horizontal row, aligned along their centers.

Example:

> (beside (ellipse 20 70 "solid" "gray")
          (ellipse 20 50 "solid" "darkgray")
          (ellipse 20 30 "solid" "dimgray")
          (ellipse 20 10 "solid" "black"))

image

procedure

(beside/align y-place i1 i2 is ...)  image?

  y-place : y-place?   i1 : image?   i2 : image?   is : image?
Constructs an image by placing all of the argument images in a horizontal row, lined up as indicated by the y-place argument. For example, if y-place is "middle", then the images are placed side by side with their centers lined up with each other.

Examples:

> (beside/align "bottom"
                (ellipse 20 70 "solid" "lightsteelblue")
                (ellipse 20 50 "solid" "mediumslateblue")
                (ellipse 20 30 "solid" "slateblue")
                (ellipse 20 10 "solid" "navy"))

image

> (beside/align "top"
                (ellipse 20 70 "solid" "mediumorchid")
                (ellipse 20 50 "solid" "darkorchid")
                (ellipse 20 30 "solid" "purple")
                (ellipse 20 10 "solid" "indigo"))

image

> (beside/align "baseline"
                (text "ijy" 18 "black")
                (text "ijy" 24 "black"))

image

procedure

(above i1 i2 is ...)  image?

  i1 : image?   i2 : image?   is : image?
Constructs an image by placing all of the argument images in a vertical row, aligned along their centers.

Example:

> (above (ellipse 70 20 "solid" "gray")
         (ellipse 50 20 "solid" "darkgray")
         (ellipse 30 20 "solid" "dimgray")
         (ellipse 10 20 "solid" "black"))

image

procedure

(above/align x-place i1 i2 is ...)  image?

  x-place : x-place?   i1 : image?   i2 : image?   is : image?
Constructs an image by placing all of the argument images in a vertical row, lined up as indicated by the x-place argument. For example, if x-place is "middle", then the images are placed above each other with their centers lined up.

Examples:

> (above/align "right"
               (ellipse 70 20 "solid" "gold")
               (ellipse 50 20 "solid" "goldenrod")
               (ellipse 30 20 "solid" "darkgoldenrod")
               (ellipse 10 20 "solid" "sienna"))

image

> (above/align "left"
               (ellipse 70 20 "solid" "yellowgreen")
               (ellipse 50 20 "solid" "olivedrab")
               (ellipse 30 20 "solid" "darkolivegreen")
               (ellipse 10 20 "solid" "darkgreen"))

image

procedure

(rotate angle image)  image?

  angle : angle?   image : image?
Rotates image by angle degrees in a counter-clockwise direction.

Examples:

> (rotate 45 (ellipse 60 20 "solid" "olivedrab"))

image

> (rotate 5 (rectangle 50 50 "outline" "black"))

image

> (rotate 45
          (beside/align
           "center"
           (rectangle 40 20 "solid" "darkseagreen")
           (rectangle 20 100 "solid" "darkseagreen")))

image

procedure

(scale factor image)  image?

  factor : (and/c real? positive?)   image : image?
Scales image by factor.

Examples:

> (scale 2 (ellipse 20 30 "solid" "blue"))

image

> (ellipse 40 60 "solid" "blue")

image

procedure

(scale/xy x-factor y-factor image)  image?

  x-factor : (and/c real? positive?)   y-factor : (and/c real? positive?)   image : image?
Scales image by x-factor horizontally and by y-factor vertically.

Examples:

> (scale/xy 3
            2
            (ellipse 20 30 "solid" "blue"))

image

> (ellipse 60 60 "solid" "blue")

image

procedure

(crop x y width height image)  image?

  x : real?   y : real?   width : (and/c real? (not/c negative?))   height : (and/c real? (not/c negative?))   image : image?
Crops image to the rectangle with the upper left at the point (x,y) and with width and height.

Examples:

> (crop 0 0 40 40 (circle 40 "solid" "chocolate"))

image

> (crop 40 60 40 60 (ellipse 80 120 "solid" "dodgerblue"))

image

> (above
   (beside (crop 40 40 40 40 (circle 40 "solid" "palevioletred"))
           (crop 0 40 40 40 (circle 40 "solid" "lightcoral")))
   (beside (crop 40 0 40 40 (circle 40 "solid" "lightcoral"))
           (crop 0 0 40 40 (circle 40 "solid" "palevioletred"))))

image

procedure

(frame image)  image?

  image : image?
Returns an image just like image, except with a black, single pixel frame drawn around the bounding box of the image.

Example:

> (frame (ellipse 40 40 "solid" "gray"))

image

Generally speaking, this function is useful to debug image constructions, i.e., to see where certain sub-images appear within some larger image.

Example:

> (beside
   (ellipse 20 70 "solid" "lightsteelblue")
   (frame (ellipse 20 50 "solid" "mediumslateblue"))
   (ellipse 20 30 "solid" "slateblue")
   (ellipse 20 10 "solid" "navy"))

image

procedure

(flip-horizontal image)  image?

  image : image?
Flips image left to right.

Flipping images with text is not supported (so passing flip-horizontal an image that contains a text or text/font image inside somewhere signals an error).

Example:

> (beside
   (rotate 30 (square 50 "solid" "red"))
   (flip-horizontal
    (rotate 30 (square 50 "solid" "blue"))))

image

procedure

(flip-vertical image)  image?

  image : image?
Flips image top to bottom.

Flipping images with text is not supported (so passing flip-vertical an image that contains a text or text/font image inside somewhere signals an error).

Example:

> (above
   (star 40 "solid" "firebrick")
   (scale/xy 1 1/2 (flip-vertical (star 40 "solid" "gray"))))

image

procedure

(text string font-size color)  image?

  string : string?   font-size : (and/c integer? (<=/c 1 255))   color : image-color?
Constructs an image that draws the given string, using the font size and color.

Examples:

> (text "Hello" 24 "olive")

image

> (text "Goodbye" 36 "indigo")

image

procedure

(text/font   string        
    font-size        
    color        
    face        
    family        
    style        
    weight        
    underline?)     image?
  string : string?
  font-size : (and/c integer? (<=/c 1 255))
  color : image-color?
  face : (or/c string? #f)
  family : (or/c 'default 'decorative 'roman 'script 'swiss 'modern 'symbol 'system)
  style : (or/c 'normal 'italic 'slant)
  weight : (or/c 'normal 'bold 'light)
  underline? : any/c
Constructs an image that draws the given string, using a complete font specification.

The face and the family combine to give the complete typeface. If face is available on the system, it is used, but if not then a default typeface based on the family is chosen. The style controls if the face is italic or not (on Windows and Mac OS X, 'slant and 'italic are the same), the weight controls if it is boldface (or light), and underline? determines if the face is underlined. For more details on these arguments, see font%, which ultimately is what this code uses to draw the font.

Examples:

> (text/font "Hello" 24 "olive"
             "Gill Sans" 'swiss 'normal 'bold #f)

image

> (text/font "Goodbye" 18 "indigo"
             #f 'modern 'italic 'normal #f)

image

> (text/font "not really a link" 18 "blue"
             #f 'roman 'normal 'normal #t)

image

procedure

(bitmap/url url)  image?

  url : string?
Goes out on the web and downloads the image at url.

Downloading the image happens each time this function is called, so you may find it simpler to download the image once with a browser and then paste it into your program or download it and use bitmap.

procedure

(image? x)  boolean?

  x : any/c
Determines if x is an image. Images are returned by functions like ellipse and rectangle and accepted by functions like overlay and beside.

Additionally, images inserted into a DrRacket window are treated as bitmap images, as are instances of image-snip% and bitmap%.

procedure

(image=? x y)  boolean?

  x : image?   y : image?
Are x and y the same image?

procedure

(image-width i)  (and/c integer? (not/c negative?) exact?)

  i : image?
Returns the width of i.

Examples:

> (image-width (ellipse 30 40 "solid" "orange"))

30

> (image-width (circle 30 "solid" "orange"))

60

> (image-width (beside (circle 20 "solid" "orange")
                       (circle 20 "solid" "purple")))

80

> (image-width (rectangle 0 10 "solid" "purple"))

0

procedure

(image-height i)  (and/c integer? (not/c negative?) exact?)

  i : image?
Returns the height of i.

Examples:

> (image-height (ellipse 30 40 "solid" "orange"))

40

> (image-height (circle 30 "solid" "orange"))

60

> (image-height (overlay (circle 20 "solid" "orange")
                         (circle 30 "solid" "purple")))

60

> (image-height (rectangle 10 0 "solid" "purple"))

0

procedure

(image->color-list image)  (listof color?)

  image : image?
Returns a list of colors that correspond to the colors in the image, reading from left to right, top to bottom.

Examples:

> (image->color-list (rectangle 2 2 "solid" "black"))

(list (color 0 0 0 255) (color 0 0 0 255) (color 0 0 0 255) (color 0 0 0 255))

> (image->color-list
   (above (beside (rectangle 1 1 "solid" (make-color 1 1 1))
                  (rectangle 1 1 "solid" (make-color 2 2 2)))
          (beside (rectangle 1 1 "solid" (make-color 3 3 3))
                  (rectangle 1 1 "solid" (make-color 4 4 4)))))

(list (color 1 1 1 255) (color 2 2 2 255) (color 3 3 3 255) (color 4 4 4 255))

procedure

(color-list->image l width height x y)  image?

  l : List-of-color   width : natural-number/c   height : natural-number/c   x : natural-number/c   y : natural-number/c
Converts a list of colors l to an image with the given width and height and pinhole (x,y) coordinates, specified with respect to the top-left of the image.

The remaining functions provide alpha-channel information as well. Alpha channels are a measure of transparency; 0 indicates fully opaque and 255 indicates fully transparent.

procedure

(image-baseline i)  (and/c integer? (not/c negative?) exact?)

  i : image?
Returns the distance from the top of the image to its baseline. The baseline of an image is the place where the bottoms any letters line up, but without counting the descenders, e.g. the tail on “y” or “g” or “j”.

Unless the image was constructed with text, text/font or, in some cases, crop, this will be the same as its height.

Examples:

> (image-baseline (text "Hello" 24 "black"))

18

> (image-height (text "Hello" 24 "black"))

24

> (image-baseline (rectangle 100 100 "solid" "black"))

100

> (image-height (rectangle 100 100 "solid" "black"))

100

A cropped image’s baseline is the same as the image’s baseline, if the cropping stays within the original image’s bounding box. But if the cropping actually enlarges the image, then the baseline can end up being smaller.

Examples:

> (image-height (rectangle 20 20 "solid" "black"))

20

> (image-baseline (rectangle 20 20 "solid" "black"))

20

> (image-height (crop 10 10 5 5 (rectangle 20 20 "solid" "black")))

5

> (image-baseline (crop 10 10 5 5 (rectangle 20 20 "solid" "black")))

5

> (image-height (crop 10 10 30 30 (rectangle 20 20 "solid" "black")))

30

> (image-baseline (crop 10 10 30 30 (rectangle 20 20 "solid" "black")))

20

procedure

(mode? x)  boolean?

  x : any/c
Determines if x is a mode suitable for constructing images.

It can be one of 'solid, "solid", 'outline, or "outline", indicating if the shape is filled in or not.

It can also be an integer between 0 and 255 (inclusive) indicating the transparency of the image. The integer 255 is fully opaque, and is the same as "solid" (or 'solid). The integer 0 means fully transparent.

procedure

(image-color? x)  boolean?

  x : any/c
Determines if x represents a color. Strings, symbols, and color structs are allowed as colors.

For example, "magenta", "black", 'orange, and 'purple are allowed. Colors are not case-sensitive, so "Magenta", "Black", 'Orange, and 'Purple are also allowed, and are the same colors as in the previous sentence. If a string or symbol color name is not recognized, black is used in its place.

The complete list of colors is available in the documentation for color-database<%>.

procedure

(x-place? x)  boolean?

  x : any/c
Determines if x is a placement option for the horizontal direction. It can be one of "left", 'left, "right", 'right, "middle", 'middle, "center", 'center, "pinhole", or 'pinhole.

Using "pinhole" or 'pinhole is only allowed when all of the image arguments have pinholes.

procedure

(y-place? x)  boolean?

  x : any/c
Determines if x is a placement option for the vertical direction. It can be one of "top", 'top, "bottom", 'bottom, "middle", 'middle, "center", 'center, "baseline", 'baseline, "pinhole", or 'pinhole.

Using "pinhole" or 'pinhole is only allowed when all of the image arguments have pinholes.

See also image-baseline for more discussion of baselines.

procedure

(angle? x)  boolean?

  x : any/c
Determines if x is an angle, namely a real number between 0 (inclusive) and 360 (exclusive).

procedure

(side-count? x)  boolean?

  x : any/c
Determines if x is an integer greater than or equal to 3.

procedure

(step-count? x)  boolean?

  x : any/c
Determines if x is an integer greater than or equal to 1.

4 Basic operations

syntax

(check-expect expression expected-expression)

Checks that the first expression evaluates to the same value as the expected-expression.
As a convenience, the name EXAMPLE is an alias for check-expect.

(check-expect (+ 1 2) 3)

procedure

(* x ...)  number

  x : number
Multiplies all given numbers. In ISL and up: * works when applied to only one number or none.
> (* 5 3)

15

> (* 5 3 2)

30

> (* 2)

2

> (*)

1

procedure

(+ x ...)  number

  x : number
Adds all given numbers. In ISL and up: + works when applied to only one number or none.
> (+ 2/3 1/16)

35/48

> (+ 3 2 5 8)

18

> (+ 1)

1

> (+)

0

procedure

(- x y ...)  number

  x : number   y : number
Subtracts the second (and following) number(s) from the first ; negates the number if there is only one argument.
> (- 5)

-5

> (- 5 3)

2

> (- 5 3 1)

1

procedure

(/ x y ...)  number

  x : number   y : number
Divides the first by all remaining numbers. In ISL and up: / computes the inverse when applied to only one number.

procedure

(< x y z ...)  boolean?

  x : real   y : real   z : real
Compares (real) numbers for less-than.
> (< 42 2/5)

false

procedure

(<= x y z ...)  boolean?

  x : real   y : real   z : real
Compares (real) numbers for less-than or equality.
> (<= 42 2/5)

false

procedure

(= x y z ...)  boolean?

  x : number   y : number   z : number
Compares numbers for equality.
> (= 42 2/5)

false

procedure

(=~ x y z)  boolean?

  x : number   y : number   z : non-negative-real
Checks whether x and y are within z of either other.
> (=~ 1.01 1.0 0.1)

true

> (=~ 1.01 1.5 0.1)

false

procedure

(> x y z ...)  boolean?

  x : real   y : real   z : real
Compares (real) numbers for greater-than.
> (> 42 2/5)

true

procedure

(>= x y z ...)  boolean?

  x : real   y : real   z : real
Compares (real) numbers for greater-than or equality.
> (>= 42 42)

true

procedure

(and x y ...)  boolean?

  x : boolean   y : boolean
Evaluates to #true if all the expressions are #true. If any expression is #false, the and expression evaluates to #false (and the expressions to the right of that expression are not evaluated.)

If any of the expressions evaluate to a value other than #true or #false, and reports an error.

> (and true false)

false

procedure

(or x y ...)  boolean?

  x : boolean   y : boolean
Evaluates to #true as soon as one of the expressions is #true (and the expressions to the right of that expression are not evaluated). If all of the expressions are #false, the or expression evaluates to #false.

If any of the expressions evaluate to a value other than #true or #false, or reports an error.

> (or true false)

true

procedure

(abs x)  real

  x : real
Determines the absolute value of a real number.
> (abs -12)

12

procedure

(acos x)  number

  x : number
Computes the arccosine (inverse of cos) of a number.
> (acos 0)

#i1.5707963267948966

procedure

(add1 x)  number

  x : number
Incrementes the given number.
> (add1 2)

3

procedure

(andmap p? l)  boolean

  p? : (X -> boolean)   l : (listof X)
Determines whether p? holds for all items of l:
(andmap p (list x-1 ... x-n)) = (and (p x-1) ... (p x-n))
> (andmap odd? '(1 3 5 7 9))

true

> threshold

3

> (andmap (lambda (x) (< x threshold)) '(0 1 2))

true

> (andmap even? '())

true

procedure

(angle x)  real

  x : number
Extracts the angle from a complex number.
> (angle (make-polar 3 4))

#i-2.2831853071795867

procedure

(append l ...)  (listof any)

  l : (listof any)
Creates a single list from several. In ASL, list* also deals with cyclic lists.

procedure

(asin x)  number

  x : number
Computes the arcsine (inverse of sin) of a number.
> (asin 0)

0

procedure

(atan x y)  number

  x : number   y : number
Computes the arctan of the given number or the ratio of the two given numbers.
> (atan 0)

0

> (atan 3 4)

#i0.6435011087932844

procedure

(boolean=? x y)  boolean?

  x : boolean?   y : boolean?
Determines whether two booleans are equal.
> (boolean=? true false)

false

procedure

(boolean? x)  boolean?

  x : any/c
Determines whether some value is a boolean.
> (boolean? 42)

false

> (boolean? false)

true

procedure

(build-list n f)  (listof X)

  n : nat   f : (nat -> X)
Constructs a list by applying f to the numbers between 0 and (- n 1):
(build-list n f) = (list (f 0) ... (f (- n 1)))
> (build-list 22 add1)

(list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22)

> i

3

> (build-list 3 (lambda (j) (+ j i)))

(list 3 4 5)

> (build-list 5
    (lambda (i)
      (build-list 5
        (lambda (j)
          (if (= i j) 1 0)))))

(list

 (list 1 0 0 0 0)

 (list 0 1 0 0 0)

 (list 0 0 1 0 0)

 (list 0 0 0 1 0)

 (list 0 0 0 0 1))

procedure

(caaar x)  any/c

  x : list?
LISP-style selector: (car (car (car (car x)))).
> w

(list (list (list (list "bye") 3) true) 42)

> (caaar w)

(list "bye")

procedure

(caadr x)  any/c

  x : list?
LISP-style selector: (car (car (cdr x))).
> z

(list (list (list (list 'a 'b) 2 3)) (list false true) "world")

> (caadr z)

false

procedure

(caar x)  any/c

  x : list?
LISP-style selector: (car (car x)).
> y

(list (list (list 1 2 3) false "world"))

> (caar y)

(list 1 2 3)

procedure

(cadar x)  any/c

  x : list?
LISP-style selector: (car (cdr (car x))).
> w

(list (list (list (list "bye") 3) true) 42)

> (cadar w)

true

procedure

(cadddr x)  any/c

  x : list?
LISP-style selector: (car (cdr (cdr (cdr x)))).
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (cadddr v)

4

procedure

(caddr x)  any/c

  x : list?
LISP-style selector: (caddr x).
> x

(list 2 "hello" true)

> (caddr x)

true

procedure

(cadr x)  any/c

  x : list?
LISP-style selector: (car (cdr x)).
> x

(list 2 "hello" true)

> (cadr x)

"hello"

procedure

(car x)  any/c

  x : cons?
Selects the first item of a non-empty list.
> x

(list 2 "hello" true)

> (car x)

2

procedure

(cdaar x)  any/c

  x : list?
LISP-style selector: (cdr (cadr (car x))).
> w

(list (list (list (list "bye") 3) true) 42)

> (cdaar w)

(list 3)

procedure

(cdadr x)  any/c

  x : list?
LISP-style selector: (cdr (car (cdr x))).
> z

(list (list (list (list 'a 'b) 2 3)) (list false true) "world")

> (cdadr z)

(list true)

procedure

(cdar x)  list?

  x : list?
LISP-style selector: (cdr (car x)).
> y

(list (list (list 1 2 3) false "world"))

> (cdar y)

(list false "world")

procedure

(cddar x)  any/c

  x : list?
LISP-style selector: (cdr (cdr (car x)))
> w

(list (list (list (list "bye") 3) true) 42)

> (cddar w)

empty

procedure

(cdddr x)  any/c

  x : list?
LISP-style selector: (cdr (cdr (cdr x))).
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (cdddr v)

(list 4 5 6 7 8 9 'A)

procedure

(cddr x)  list?

  x : list?
LISP-style selector: (cdr (cdr x)).
> x

(list 2 "hello" true)

> (cddr x)

(list true)

procedure

(cdr x)  any/c

  x : cons?
Selects the rest of a non-empty list.
> x

(list 2 "hello" true)

> (cdr x)

(list "hello" true)

procedure

(ceiling x)  integer

  x : real
Determines the closest integer (exact or inexact) above a real number. See round.
> (ceiling 12.3)

#i13.0

procedure

(char->integer c)  integer

  c : char
Lookups the number that corresponds to the given character in the ASCII table (if any).
> (char->integer #\a)

97

> (char->integer #\z)

122

procedure

(char-alphabetic? c)  boolean?

  c : char
Determines whether a character represents an alphabetic character.
> (char-alphabetic? #\Q)

true

procedure

(char-ci<=? c d e ...)  boolean?

  c : char   d : char   e : char
Determines whether the characterc are ordered in an increasing and case-insensitive manner.
> (char-ci<=? #\b #\B)

true

> (char<=? #\b #\B)

false

procedure

(char-ci<? c d e ...)  boolean?

  c : char   d : char   e : char
Determines whether the characterc are ordered in a strictly increasing and case-insensitive manner.
> (char-ci<? #\B #\c)

true

> (char<? #\b #\B)

false

procedure

(char-ci=? c d e ...)  boolean?

  c : char   d : char   e : char
Determines whether two characters are equal in a case-insensitive manner.
> (char-ci=? #\b #\B)

true

procedure

(char-ci>=? c d e ...)  boolean?

  c : char   d : char   e : char
Determines whether the characters are sorted in a decreasing and case-insensitive manner.
> (char-ci>=? #\b #\C)

false

> (char>=? #\b #\C)

true

procedure

(char-ci>? c d e ...)  boolean?

  c : char   d : char   e : char
Determines whether the characters are sorted in a strictly decreasing and case-insensitive manner.
> (char-ci>? #\b #\B)

false

> (char>? #\b #\B)

true

procedure

(char-downcase c)  char

  c : char
Produces the equivalent lower-case character.
> (char-downcase #\T)

#\t

procedure

(char-lower-case? c)  boolean?

  c : char
Determines whether a character is a lower-case character.
> (char-lower-case? #\T)

false

procedure

(char-numeric? c)  boolean?

  c : char
Determines whether a character represents a digit.
> (char-numeric? #\9)

true

procedure

(char-upcase c)  char

  c : char
Produces the equivalent upper-case character.
> (char-upcase #\t)

#\T

procedure

(char-upper-case? c)  boolean?

  c : char
Determines whether a character is an upper-case character.
> (char-upper-case? #\T)

true

procedure

(char-whitespace? c)  boolean?

  c : char
Determines whether a character represents space.
> (char-whitespace? #\tab)

true

procedure

(char<=? c d e ...)  boolean?

  c : char   d : char   e : char
Determines whether the characterc are ordered in a strictly increasing manner.
> (char<=? #\a #\a #\b)

true

procedure

(char<? x d e ...)  boolean?

  x : char   d : char   e : char
Determines whether the characterc are ordered in a strictly increasing manner.
> (char<? #\a #\b #\c)

true

procedure

(char=? c d e ...)  boolean?

  c : char   d : char   e : char
Determines whether the characters are equal.
> (char=? #\b #\a)

false

procedure

(char>=? c d e ...)  boolean?

  c : char   d : char   e : char
Determines whether the characters are sorted in a decreasing manner.
> (char>=? #\b #\b #\a)

true

procedure

(char>? c d e ...)  boolean?

  c : char   d : char   e : char
Determines whether the characters are sorted in a strictly decreasing manner.
> (char>? #\A #\z #\a)

false

procedure

(char? x)  boolean?

  x : any/c
Determines whether a value is a character.

procedure

(complex? x)  boolean?

  x : any/c
Determines whether some value is complex.
> (real? 1-2i)

false

procedure

(conjugate x)  number

  x : number
Extracts the conjugate of a complex number.
> (conjugate (make-polar 3 4))

#i-1.960930862590836+2.2704074859237844i

procedure

(cons x l)  (listof X)

  x : X   l : (listof X)
Constructs a list. In ASL, cons creates a mutable list.

procedure

(cons? x)  boolean?

  x : any/c
Determines whether some value is a constructed list.
> (cons? (cons 1 empty))

true

> (cons? 42)

false

procedure

(cos x)  number

  x : number
Computes the cosine of a number (radians).
> (cos pi)

#i-1.0

procedure

(cosh x)  number

  x : number
Computes the hyperbolic cosine of a number.
> (cosh 10)

#i11013.232920103324

procedure

(current-seconds)  integer

Determines the current time in seconds elapsed (since a platform-specific starting date).
> (current-seconds)

1359841903

procedure

(denominator x)  integer

  x : rational?
Computees the denominator of a rational.
> (denominator 2/3)

3

value

e : real

Euler’s number.
> e

#i2.718281828459045

procedure

(eighth x)  any/c

  x : list?
Selects the eighth item of a non-empty list.
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (eighth v)

8

value

empty : empty?

The empty list.

procedure

(empty? x)  boolean?

  x : any/c
Determines whether some value is the empty list.
> (empty? empty)

true

> (empty? 42)

false

value

eof : eof-object?

A value that represents the end of a file:
> eof

#<eof>

procedure

(eof-object? x)  boolean?

  x : any/c
Determines whether some value is the end-of-file value.
> (eof-object? eof)

true

> (eof-object? 42)

false

procedure

(eq? x y)  boolean?

  x : any/c   y : any/c
Determines whether two values are equivalent from the computer’s perspective (intensional).
> (eq? (cons 1 empty) (cons 1 empty))

false

> one

(list 1)

> (eq? one one)

true

procedure

(equal? x y)  boolean?

  x : any/c   y : any/c
Determines whether two values are structurally equal where basic values are compared with the eqv? predicate.
> (equal? (make-posn 1 2) (make-posn (- 2 1) (+ 1 1)))

true

procedure

(equal~? x y z)  boolean?

  x : any/c   y : any/c   z : non-negative-real
Compares x and y like equal? but uses =~ in the case of numbers.
> (equal~? (make-posn 1.01 1.0) (make-posn 1.01 0.99) 0.2)

true

procedure

(eqv? x y)  boolean?

  x : any/c   y : any/c
Determines whether two values are equivalent from the perspective of all functions that can be applied to it (extensional).
> (eqv? (cons 1 empty) (cons 1 empty))

false

> one

(list 1)

> (eqv? one one)

true

procedure

(error x ...)  void?

  x : any/c
Signals an error, combining the given values into an error message. If any of the values’ printed representations is too long, it is truncated and “...” is put into the string. If the first value is a symbol, it is suffixed with a colon and the result pre-pended on to the error message.
> zero

0

> (if (= zero 0) (error "can't divide by 0") (/ 1 zero))

can't divide by 0

procedure

(even? x)  boolean?

  x : integer
Determines if some integer (exact or inexact) is even or not.
> (even? 2)

true

procedure

(exact->inexact x)  number

  x : number
Converts an exact number to an inexact one.
> (exact->inexact 12)

#i12.0

procedure

(exp x)  number

  x : number
Determines e raised to a number.
> (exp -2)

#i0.1353352832366127

procedure

(expt x y)  number

  x : number   y : number
Computes the power of the first to the second number.
> (expt 16 1/2)

4

> (expt 3 -4)

1/81

value

false : boolean?

The false value.

procedure

(false? x)  boolean?

  x : any/c
Determines whether a value is false.
> (false? false)

true

procedure

(fifth x)  any/c

  x : list?
Selects the fifth item of a non-empty list.
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (fifth v)

5

procedure

(first x)  any/c

  x : cons?
Selects the first item of a non-empty list.
> x

(list 2 "hello" true)

> (first x)

2

procedure

(floor x)  integer

  x : real
Determines the closest integer (exact or inexact) below a real number. See round.
> (floor 12.3)

#i12.0

procedure

(foldl f base l)  Y

  f : (X Y -> Y)   base : Y   l : (listof X)
(foldl f base (list x-1 ... x-n)) = (f x-n ... (f x-1 base))
> (foldl + 0 '(0 1 2 3 4 5 6 7 8 9))

45

> a-list

(list 0 1 2 3 4 5 6 7 8 9)

> (foldl (lambda (x r) (if (> x threshold) (cons (* 2 x) r) r)) '() a-list)

(list 18 16 14 12 10 8)

procedure

(format f x ...)  string

  f : string   x : any/c
Formats a string, possibly embedding values.
> (format "Dear Dr. ~a:" "Flatt")

"Dear Dr. Flatt:"

> (format "Dear Dr. ~s:" "Flatt")

"Dear Dr. \"Flatt\":"

> (format "the value of ~s is ~a" '(+ 1 1) (+ 1 1))

"the value of (+ 1 1) is 2"

procedure

(fourth x)  any/c

  x : list?
Selects the fourth item of a non-empty list.
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (fourth v)

4

procedure

(gcd x y ...)  integer

  x : integer   y : integer
Determines the greatest common divisior of two integers (exact or inexact).
> (gcd 6 12 8)

2

procedure

(identity x)  any

  x : any/c
Returns x.
> (identity 42)

42

> (identity c1)

image

> (identity "hello")

"hello"

procedure

(imag-part x)  real

  x : number
Extracts the imaginary part from a complex number.
> (imag-part 3+4i)

4

procedure

(inexact->exact x)  number

  x : number
Approximates an inexact number by an exact one.
> (inexact->exact 12.0)

12

procedure

(inexact? x)  boolean?

  x : number
Determines whether some number is inexact.
> (inexact? 1-2i)

false

procedure

(integer->char x)  char

  x : exact-integer?
Lookups the character that corresponds to the given exact integer in the ASCII table (if any).
> (integer->char 42)

#\*

procedure

(integer? x)  boolean?

  x : any/c
Determines whether some value is an integer (exact or inexact).
> (integer? (sqrt 2))

false

procedure

(lcm x y ...)  integer

  x : integer   y : integer
Determines the least common multiple of two integers (exact or inexact).
> (lcm 6 12 8)

24

procedure

(length l)  natural-number?

  l : list?
Evaluates the number of items on a list.
> x

(list 2 "hello" true)

> (length x)

3

procedure

(list x ...)  list?

  x : any/c
Constructs a list of its arguments.
> (list 1 2 3 4 5 6 7 8 9 0)

(cons

 1

 (cons

  2

  (cons

   3

   (cons

    4

    (cons

     5

     (cons 6 (cons 7 (cons 8 (cons 9 (cons 0 empty))))))))))

procedure

(pair? v)  boolean?

  v : any/c
Returns #t if v is a pair, #f otherwise.

procedure

(list* x ... l)  (listof any)

  x : any   l : (listof any)
Constructs a list by adding multiple items to a list. In ASL, list* also deals with cyclic lists.

procedure

(list->string l)  string

  l : list?
Converts a s list of characters into a string.
> (list->string (cons #\c (cons #\a (cons #\t empty))))

"cat"

procedure

(list-ref x i)  any/c

  x : list?   i : natural?
Extracts the indexed item from the list.
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (list-ref v 9)

'A

procedure

(log x)  number

  x : number
Determines the base-e logarithm of a number.
> (log 12)

#i2.4849066497880004

procedure

(magnitude x)  real

  x : number
Determines the magnitude of a complex number.
> (magnitude (make-polar 3 4))

#i3.0

procedure

(make-posn x y)  posn

  x : any/c   y : any/c
Constructs a posn from two arbitrary values.
> (make-posn 3 3)

(posn 3 3)

> (make-posn "hello" true)

(posn "hello" true)

procedure

(make-string i c)  string

  i : natural-number   c : char
Produces a string of length i from c.
> (make-string 3 #\d)

"ddd"

procedure

(map f l ...)  (listof Z)

  f : (X ... -> Z)   l : (listof X)
Constructs a new list by applying a function to each item on one or more existing lists:
(map f (list x-1 ... x-n)) = (list (f x-1) ... (f x-n))
> (map add1 '(3 -4.01 2/5))

(list 4 #i-3.01 1.4)

> (map (lambda (x) (list 'my-list (+ x 1))) '(3 -4.01 2/5))

(list

 (list 'my-list 4)

 (list 'my-list #i-3.01)

 (list 'my-list 1.4))

procedure

(max x y ...)  real

  x : real   y : real
Determines the largest number—aka, the maxiumum.
> (max 3 2 8 7 2 9 0)

9

procedure

(member x l)  boolean?

  x : any/c   l : list?
Determines whether some value is on the list (comparing values with equal?).
> x

(list 2 "hello" true)

> (member "hello" x)

true

procedure

(memq x l)  boolean?

  x : any/c   l : list?
Determines whether some value x is on some list l, using eq? to compare x with items on l.
> x

(list 2 "hello" true)

> (memq (list (list 1 2 3)) x)

false

procedure

(memv x l)  (or/c false list)

  x : any/c   l : list?
Determines whether some value is on the list if so, it produces the suffix of the list that starts with x if not, it produces false. (It compares values with the eqv? predicate.)
> x

(list 2 "hello" true)

> (memv (list (list 1 2 3)) x)

false

procedure

(min x y ...)  real

  x : real   y : real
Determines the smallest number—aka, the miniumum.
> (min 3 2 8 7 2 9 0)

0

procedure

(modulo x y)  integer

  x : integer   y : integer
Finds the remainder of the division of the first number by the second:
> (modulo 9 2)

1

> (modulo 3 -4)

-1

procedure

(negative? x)  boolean?

  x : number
Determines if some value is strictly smaller than zero.
> (negative? -2)

true

procedure

(not x)  boolean?

  x : boolean?
Negates a boolean value.

value

null : list

Another name for the empty list
> null

empty

procedure

(null? x)  boolean?

  x : any/c
Determines whether some value is the empty list.
> (null? empty)

true

> (null? 42)

false

procedure

(number->string x)  string

  x : number
Converts a number to a string.
> (number->string 42)

"42"

procedure

(number? n)  boolean?

  n : any/c
Determines whether some value is a number:
> (number? "hello world")

false

> (number? 42)

true

procedure

(numerator x)  integer

  x : rational?
Computes the numerator of a rational.
> (numerator 2/3)

2

procedure

(odd? x)  boolean?

  x : integer
Determines if some integer (exact or inexact) is odd or not.
> (odd? 2)

false

value

pi : real

The ratio of a circle’s circumference to its diameter.
> pi

#i3.141592653589793

procedure

(positive? x)  boolean?

  x : number
Determines if some value is strictly larger than zero.
> (positive? -2)

false

procedure

(posn-x p)  any

  p : posn
Extracts the x component of a posn.
> p

(posn 2 -3)

> (posn-x p)

2

procedure

(posn-y p)  any

  p : posn
Extracts the y component of a posn.
> p

(posn 2 -3)

> (posn-y p)

-3

procedure

(posn? x)  boolean?

  x : any/c
Determines if its input is a posn.
> q

(posn "bye" 2)

> (posn? q)

true

> (posn? 42)

false

procedure

(quotient x y)  integer

  x : integer   y : integer
Divides the second integer—also called divisor—into the first—known as dividend—to obtain the quotient.
> (quotient 9 2)

4

> (quotient 3 4)

0

procedure

(random x)  integer

  x : integer
Generates a random natural number less than some given integer In ASL: random generate a random inexact number between 0.0 and 1.0 exclusive when applied to no argument.
> (random)

#i0.183137602892849

procedure

(rational? x)  boolean?

  x : any/c
Determines whether some value is a rational number.
> (rational? 1-2i)

false

procedure

(real-part x)  real

  x : number
Extracts the real part from a complex number.
> (real-part 3+4i)

3

procedure

(real? x)  boolean?

  x : any/c
Determines whether some value is a real number.
> (real? 1-2i)

false

procedure

(remainder x y)  integer

  x : integer   y : integer
Determines the remainder of dividing the first by the second integer (exact or inexact).
> (remainder 9 2)

1

> (remainder 3 4)

3

procedure

(rest x)  any/c

  x : cons?
Selects the rest of a non-empty list.
> x

(list 2 "hello" true)

> (rest x)

(list "hello" true)

procedure

(reverse l)  list

  l : list?
Creates a reversed version of a list.
> x

(list 2 "hello" true)

> (reverse x)

(list true "hello" 2)

procedure

(round x)  integer

  x : real
Rounds a real number to an integer (rounds to even to break ties). See floor and ceiling.
> (round 12.3)

#i12.0

procedure

(second x)  any/c

  x : list?
Selects the second item of a non-empty list.
> x

(list 2 "hello" true)

> (second x)

"hello"

procedure

(seventh x)  any/c

  x : list?
Selects the seventh item of a non-empty list.
> v

(list 1 2 3 4 5 6 7 8 9 'A)

> (seventh v)

7

procedure

(sgn x)  (union 1 1.0 0 0.0 -1 -1.0)

  x : real
Determines the sign of a real number.
> (sgn -12)

-1

procedure

(sin x)  number

  x : number
Computes the sine of a number (radians).
> (sin pi)

#i1.2246467991473532e-16

procedure

(sinh x)  number

  x : number
Computes the hyperbolic sine of a number.
> (sinh 10)

#i11013.232874703393

procedure