Web Primitives Reference

The web interpreter contains all primitives in the staff interpreter, with the following additional primitives.

This page is still under construction.

Saving and Loading

demo

(demo <name> [show-names])

Loads the demo named name. Currently available demos include autopair, chess, diagram, go, and paint. If show-names is true, names bound by the demo will be displayed. If show-names is false or not present, they will not be displayed.

download

(download <id> [show-names])

If id is a URL, attempts to download and load Scheme code from that URL. If not, attempts to download a GitHub Gist with that id. If show-names is true or not present, names bound will be displayed. If show-names is false, they will not be displayed.

library

(library <name> [show-names])

Loads the library named name. Currently available libraries include church, hash, html, math, strings, and timing. If show-names is true or not present, names bound by the library will be displayed. If show-names is false, they will not be displayed.

list-saves

(list-saves)

Returns a list of all files saved with save or the editor.

load

(load <filename> [show-names])

Loads a file named filename that was previously saved with save or the editor. If show-names is true or not present, names bound by the file will be displayed. If show-names is false, they will not be displayed.

edit

(edit <filename>)

Opens the editor to the file filename.

save

(save <filename>)

Saves all input from this interpreter session into a file filename.

upload

(upload [filename])

If filename exists, uploads the code in that file to a GitHub Gist. If it does not exist, uploads all input from this interpreter session.

Diagramming and Visualization

breakpoint

(breakpoint)

When running debug, this will pause the debugger. Ignored otherwise.

debug

This procedure acts like a macro or special form, as it's operands are not evaluated when called.

(debug <code> ...)

Runs any code provided as operands in the debugger. Clicking the "Step" button will evaluate code until the next time a name is bound or a frame is created or returns. Click the "Continue" button will evaluate code until breakpoint is called or execution completes. The current environment diagram will be updated as execution progresses.

diagram

(diagram)

Draws a diagram of the environment in which this is called.

draw-pair

(draw-pair <pair>)

Draws a box-and-pointer diagram of pair, which must be a pair or vector.

exit-diagram

(exit-diagram)

Closes any diagram drawn by debug, diagram, draw-pair, or visualize.

save-diagram

(save-diagram)

Attempts to convert the diagram to an image that can be saved.

Works best with the monochrome theme.

visualize

This procedure acts like a macro or special form, as it's operands are not evaluated when called.

(visualize <code> ...)

Runs any code provided as operands, storing a snapshot of the environment each time a new frame is created, a procedure returns, or a name is bound. Draws a diagram of the environment prior to any code being run and display buttons that allow the user to navigate through the steps.

Events

cancel

(cancel <event-id>)

Cancels all listeners to the event-id event.

cancel-all

(cancel-all)

Cancels all event listeners. This only affects Scheme event listeners. The HTML library uses JavaScript's native event listeners for DOM events.

create-button

(create-button <id> <text>)

Creates a button with text text that will trigger the button_<id> event with no arguments when clicked. If a button with id already exists, this will update its text.

delete-button

(delete-button <id>)

Deletes button with id if it exists.

handle

(handle <event-id> <procedure>)

Shorthand for:

(cancel <event-id>)
(listen <event-id> <procedure>)

listen

(listen <event-id> <procedure>)

Starts listening to event-id with procedure. When event-id is triggered, procedure will be called with any provided arguments. Make sure procedure accepts the right number of arguments or allows for a variable number of them.

listeners

(listeners)

Returns a list of event listeners, where each item is itself a list with the event id as its first element and the listening procedure as its second.

trigger-event

(trigger-event <event-id> [arg] ...)

Triggers with event event-id with args as its arguments.

wait

(wait <millis>)

Pauses execution of the current thread for millis milliseconds. Because the interpreter is asynchronus, this will not block the entire page. Other threads of Scheme execution (including event listeners and any additional interactive input) will continue to run.

Turtle Graphics

showturtle, hideturtle, speed and exitonclick are still bound, but have no effect. The turtle is always hidden and draws as fast as possible.

exit_turtle

(exit_turtle)

Resets the turtle and hides the turtle canvas.

external_canvas

(external_canvas <w> <h>)

Changes the size of the canvas in pixels to a width of w and a height of h. Does not clear the drawing (as the internal drawing size is unchanged).

The default canvas size is 500x500 pixels.

internal_canvas

(internal_canvas <w> <h>)

Changes the size of the internal turtle grid to a width of w units and a height of h units. Clears the drawing as the internal grid itself changes.

The default grid size is 1000x1000 units.

turtle_help

(turtle_help)

Displays a brief summary of the additional turtle primitives on the web.

Alias: exitturtle

Theming

reset-theme

(reset-theme)

Restores all theme settings to the default.

set-custom-css

(set-custom-css <css>)

Adds a string of CSS code css to the page. Subsequent calls will override it.

theme

(theme <name>)

Loads one of the built-in themes. Currently available themes include solarized, monokai, monochrome, and gobears.

ui-border

(ui-border <key> <value>)

Sets the CSS border property of element named key to value. Key can be one of current-frame, other-frame, box, button, and button-hover.

ui-color

(ui-color <key> <value>)

Sets the color of an element named key to value. value can be any color supported by the turtle primitives or any valid CSS color. Key can be one of normal-keyword, special-keyword, turtle-keyword, text, number, boolean, string, procedure, symbol, comment, error, link, link-hover, current-frame, other-frame, pair, vector, promise, diagram-text, return-value, arrow, prompt, loading-prompt, status, background, button, button-hover, button-text, or button-text-hover.

ui-text-style

(ui-text-style <key> <css>)

Adds any string of CSS (such as font-weight: bold; font-style: italic;) to the element named key. Key can be one of normal-keyword, special-keyword, turtle-keyword, text, number, boolean, string, procedure, symbol, comment, error, link, link-hover, diagram-text, return-value, prompt, loading-prompt, status, button, and button-hover.

zoom

(zoom <scale>)

Sets the scale of the entire interpreter to a factor of scale (where 1 is the default).

Vectors

make-vector

(make-vector <size>)

Creates a new vector of length size with elements initialized to nil.

list->vector

(list->vector <lst>)

Converts a list lst to a vector with the same elements.

vector

(vector [item]...)

Creates a new vector with items as its elements.

vector?

(vector? <arg>)

Returns true if arg is a vector; false otherwise.

vector->list

(vector->list <vec>)

Converts a vector vec to a list with the same elements.

vector-length

(vector-length <vec>)

Returns the length of vector vec.

vector-ref

(vector-ref <vec> <index>)

Returns the element at index index of vector vec.

vector-set!

(vector-set! <vec> <index> <val>)

Sets the element at index index of vector vec to be equal to val.

Logic

logic

(logic)

Binds the fact and query procedures below in the current environment. All calls to these procedures made from of the same call to logic will use the same Logic environment.

fact

This procedure acts like a macro or special form, as it's operands are not evaluated when called.

(fact <f>)

Declares f as a fact in Logic.

Alias: !

query

This procedure acts like a macro or special form, as it's operands are not evaluated when called.

(query <q>)

Looks up q in Logic.

Alias: ?

logic-query

(logic-query <facts> <query>)

Looks up query in a Logic environment based entirely upon facts. Returns a list of valid sets of bindings.

scm> (logic-query '(((likes john dogs))) '((likes ?who dogs)))
((("?who" "john")))

JS Interop

For more information about JS interop, see here. That document also describes how values are converted between JS and Scheme types.

js

(js <js-code> ...)

Concatenates all arguments into a string of JS code that is run through the JS eval function, returning the result.

js-call

(js <obj> <method> [arg] ...)

Calls the method method on the JS object obj with any provided args, returning the result.

js-has-property?

(js-has-property? <obj> <property>)

Returns true if the JS object obj has the property property.

js-object

(js-object [kv-pair] ...)

Where each kv-pair is a pair with a car of key and a cdr of value.

Creates a new JS object with all keys bound to their corresponding values.

OR

(js-object <type> [arg] ...)

Calls the constructor for a JS object of type type with any given args, returning the result.

js-object?

(js-object? <arg>)

Returns true if arg is a JS object. Through technically true, this will return false if provided with a JS function, as they are instead wrapped as Scheme procedures when passed to Scheme.

js-ref

(js-ref <obj> <prop>)

Returns the property prop of JS object obj. Note that, due to the way JS methods work, you will likely want to use js-call instead if you intend to use this to retrieve and call a method on obj.

js-set!

(js-set! <obj> <key> <value>)

Sets the property key of JS object obj to equal value.

Miscellaneous

bindings

(bindings)

Prints out all names bound in the current environment. Used primarily for debugging purposes. All names are wrapped in parentheses so that they are properly highlighted as procedures.

build-info

(build-info)

Prints out various information about how the running interpreter was built. Please include the output of this command with all bug reports.

bound?

(bound? <symbol>)

Returns true if symbol is bound as a name in the current environment.

bound-in-global?

(bound-in-global? <symbol>)

Returns true if symbol is bound as a name in the global frame.

current-environment

(current-environment)

Returns the Scheme environment the primitive is evaluated in. At the moment, there are no direct ways to manipulate environments, but, if you pass it as an extra argument at the end to any primitive that normally acts on the environment it's called in (such as eval or apply), they will instead act as if they were called in that environment.

hash-code

(hash-code <arg>)

Returns an integer hash code for arg. If two values are considered equivalent by equals?, then they will have the same hash code.

hide

(hide <symbol>)

Hides the binding for symbol in environment diagrams. Reset when the symbol is rebound. Names starting with an underscore are hidden by default.

unbind

(unbind <symbol>)

Removes the binding for symbol if it exists. Errors if it does not exist.

error-notrace

(error-notrace <msg> ...)

Equivalent to error, but does not print the stack trace. Used by the chess demo for invalid moves.

For a more detailed JS Interop guide, see here.