CS计算机代考程序代写 scheme python interpreter 11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 1/17

Scheme Built-In Procedure Reference

Last Updated: Spring 2020

This document serves as a reference for the built-in procedures in the Scheme project and

sta� Scheme interpreter (http://cs61a.org/assets/interpreter/scheme). The web interpreter

(https://scheme.cs61a.org) includes several additional built-in procedures.

In all of the syntax de�nitions below, refers to a required element x that can vary, while
[x] refers to an optional element x . Ellipses indicate that there can be more than one of the
preceding element. It is assumed for all of these built-ins that the elements represent

evaluated arguments not the literal expressions typed in.

Core Interpreter
apply

(apply )

Calls procedure with the given list of args .

scm> (apply + ‘(1 2 3))
6

On macros, this has the e�ect of calling the macro without the initial quoting or �nal

evaluation. Thus, apply treats a macro as if it were a function.

display

(display )

Prints val . If val is a Scheme string, it will be output without quotes.

A new line will not be automatically included.

displayln

(displayln )

http://cs61a.org/assets/interpreter/scheme
https://scheme.cs61a.org/
Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 2/17

Like display , but includes a newline at the end.

error

(error )

Raises an SchemeError with msg as it’s message. If there is no msg , the error’s message will be
empty.

eval

(eval )

Evaluates expression in the current environment.

scm> (eval ‘(cons 1 (cons 2 nil)))
(1 2)

exit

(exit)

Exits the interpreter. In the web interpreter, this does nothing.

load

(load )

Loads the contents of the �le with filename and evaluates the code within. filename must be
a symbol. If that �le is not found, filename .scm will be attempted.

The web interpreter’s does not currently support load . The closest analog is import-inline ,
which takes a URL and evaluates the Scheme code in the current environment.

newline

(newline)

Prints a new line.

print

(print …)

Prints the Scheme representation of each val , separated by spaces. Unlike display , this will
include the outer quotes on a Scheme string, and a newline.

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 3/17

Type Checking
atom?

(atom? )

Returns true if arg is a boolean, number, symbol, string, or nil; false otherwise.

boolean?

(boolean? )

Returns true if arg is a boolean; false otherwise.

integer?

(integer? )

Returns true if arg is a integer; false otherwise.

list?

(list? )

Returns true if arg is a well-formed list (i.e., it doesn’t contain a stream); false otherwise. If the
list has a cycle, this may cause an error or in�nite loop.

scm> (list? ‘(1 2 3))
True
scm> (list? (cons-stream 1 nil))
False

number?

(number? )

Returns true if arg is a number; false otherwise.

null?

(null? )

Returns true if arg is nil (the empty list); false otherwise.

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 4/17

pair?

(pair? )

Returns true if arg is a pair; false otherwise.

procedure?

(procedure? )

Returns true if arg is a procedure; false otherwise.

promise?

(promise? )

Returns true if arg is a promise; false otherwise.

string?

(string? )

Returns true if arg is a string; false otherwise.

symbol?

(symbol? )

Returns true if arg is a symbol; false otherwise.

Pair and List Manipulation
append

(append [lst] …)

Returns the result of appending the items of all lst s in order into a single list. Returns nil if
no lst s.

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 5/17

scm> (append ‘(1 2 3) ‘(4 5 6))
(1 2 3 4 5 6)
scm> (append)
()
scm> (append ‘(1 2 3) ‘(a b c) ‘(foo bar baz))
(1 2 3 a b c foo bar baz)
scm> (append ‘(1 2 3) 4)
Error

car

(car )

Returns the car of pair . Errors if pair is not a pair.

cdr

(cdr )

Returns the cdr of pair . Errors if pair is not a pair.

cons

(cons )

Returns a new pair with first as the car and rest as the cdr

length

(length )

Returns the length of arg . If arg is not a list, this will cause an error.

list

(list …)

Returns a list with the item s in order as its elements.

map

(map )

Returns a list constructed by calling proc (a one-argument procedure) on each item in lst .

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 6/17

filter

(filter )

Returns a list consisting of only the elements of lst that return true when called on pred (a
one-argument procedure).

reduce

(reduce )

Returns the result of sequentially combining each element in lst using combiner (a two-
argument procedure). reduce works from left-to-right, with the existing combined value passed
as the �rst argument and the new value as the second argument. lst must contain at least
one item.

Mutation

set-car!

(set-car! )

Sets the car of pair to value . pair must be a pair.

set-cdr!

(set-cdr! )

Sets the cdr of pair to value . pair must be a pair.

Arithmetic Operations
+

(+ [num] …)

Returns the sum of all num s. Returns 0 if there are none. If any num is not a number, this will
error.

(- …)

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 7/17

If there is only one num , return its negation. Otherwise, return the �rst num minus the sum of
the remaining num s. If any num is not a number, this will error.

*

(* [num] …)

Returns the product of all num s. Returns 1 if there are none. If any num is not a number, this
will error.

/

(/ [divisor] …)

If there are no divisor s, return 1 divided by dividend . Otherwise, return dividend divided by
the product of the divisors . This built-in does true division, not �oor division. dividend and all
divisor s must be numbers.

scm> (/ 4)
0.25
scm> (/ 7 2)
3.5
scm> (/ 16 2 2 2)
2

abs

(abs )

Returns the absolute value of num , which must be a number.

expt

(expt )

Returns the base raised to the power power. Both must be numbers.

modulo

(modulo )

Returns a modulo b . Both must be numbers.

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 8/17

scm> (modulo 7 3)
1
scm> (modulo -7 3)
2

quotient

(quotient )

Returns dividend integer divided by divisor . Both must be numbers.

scm> (quotient 7 3)
2

remainder

(remainder )

Returns the remainder that results when dividend is integer divided by divisor . Both must be
numbers. Di�ers from modulo in behavior when negative numbers are involved.

scm> (remainder 7 3)
1
scm> (remainder -7 3)
-1

Additional Math Procedures
The Python-based interpreter adds the following additional procedures whose behavior exactly

match the corresponding Python functions in the math module

(https://docs.python.org/3/library/math.html).

acos

acosh

asin

asinh

atan

atan2

atanh

ceil

copysign

cos

cosh

degrees

�oor

https://docs.python.org/3/library/math.html
Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 9/17

log

log10

log1p

log2

radians

sin

sinh

sqrt

tan

tanh

trunc

Boolean Operations

General

eq?

(eq? )

If a and b are both numbers, booleans, symbols, or strings, return true if they are equivalent;
false otherwise.

Otherwise, return true if a and b both refer to the same object in memory; false otherwise.

scm> (eq? ‘(1 2 3) ‘(1 2 3))
False
scm> (define x ‘(1 2 3))
scm> (eq? x x)
True

equal?

(equal? )

Returns true if a and b are equivalent. For two pairs, they are equivalent if their car s are
equivalent and their cdr s are equivalent.

scm> (equal? ‘(1 2 3) ‘(1 2 3))
True

not

(not )

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 10/17

Returns true if arg is false-y or false if arg is truthy.

On Numbers

=

(= )

Returns true if a equals b . Both must be numbers.

< (< )

Returns true if a is less than b . Both must be numbers.

>

(> )

Returns true if a is greater than b . Both must be numbers.

<= (<= )

Returns true if a is less than or equal to b . Both must be numbers.

>=

(>= )

Returns true if a is greater than or equal to b . Both must be numbers.

even?

(even? )

Returns true if num is even. num must be a number.

odd?

(odd? )

Returns true if num is odd. num must be a number.

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 11/17

zero?

(zero? )

Returns true if num is zero. num must be a number.

Promises and Streams
force

(force )

Returns the evaluated result of promise . If promise has already been forced, its expression will
not be evaluated again. Instead, the result from the previous evaluation will be returned.

promise must be a promise.

cdr-stream

(cdr-stream )

Shorthand for (force (cdr )) .

Turtle Graphics
backward

(backward )

Moves the turtle backward n units in its current direction from its current position.

Aliases: back , bk

begin_fill

(begin_fill)

Starts a sequence of moves that outline a shape to be �lled. Call end_fill to complete the �ll.

bgcolor

(bgcolor )

Sets the background color of the turtle window to a color c (same rules as when calling
color ).

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 12/17

circle

(circle [extent])

Draws a circle of radius r , centered r units to the turtle’s left. If extent exists, draw only the
�rst extent degrees of the circle. If r is positive, draw in the counterclockwise direction.
Otherwise, draw in the clockwise direction.

The web interpreter has trouble accurately drawing partial circles.

clear

(clear)

Clears the drawing, leaving the turtle unchanged.

color

(color )

Sets the pen color to c , which is a Scheme string such as “red” or “#�c0c0”.

The web interpreter also allows c to be a symbol. Available named colors may vary depending
on the interpreter.

end_fill

(end_fill)

Fill in shape drawn since last call to begin_fill .

exitonclick

(exitonclick)

In pillow-turtle mode, this exits the current program. In tk-turtle mode, it exits the current

program when the window is clicked. In the web interpreter, it closes the canvas.

In the local interpreter, you can pass –turtle-save-path PATH to also e�ectively call (save-to-
file PATH) right before exit.

forward

(forward )

Moves the turtle forward n units in its current direction from its current position.

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 13/17

Alias: fd

hideturtle

(hideturtle)

Makes the turtle invisible.

This procedure has no e�ect on the web interpreter, as the turtle is always invisible.

Alias: ht

left

(left )

Rotates the turtle’s heading n degrees counterclockwise.

Alias: lt

pendown

(pendown)

Lowers the pen so that the turtle starts drawing.

Alias: pd

penup

(penup)

Raises the pen so that the turtle does not draw.

Alias: pu

pixel

(pixel )

Draws a box �lled with pixels starting at ( x , y ) in color c (same rules as in color ). By default
the box is one pixel, though this can be changed with pixelsize .

pixelsize

(pixelsize )

Changes the size of the box drawn by pixel to be size x size .

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 14/17

rgb

(rgb )

Returns a color string formed from r , g , and b values between 0 and 1.

right

(right )

Rotates the turtle’s heading n degrees clockwise.

Alias: rt

save-to-file

(save-to-file )

Saves the current canvas to a �le speci�ed by f , with an added �le extension.

For example, (save-to-file “hi”)

saves to ./hi.png in the local interpreter using the pillow-turtle
saves to ./hi.ps in the local interpreter using the tk-turtle (default)
has no e�ect in the web interpreter

screen_width

(screen_width)

Returns the width of the turtle screen in pixels of the current size.

screen_height

(screen_height)

Returns the height of the turtle screen in pixels of the current size.

setheading

(setheading )

Sets the turtle’s heading h degrees clockwise from the north.

Alias: seth

setposition

Free Hand

Free Hand

11/6/2020 Scheme Built-In Procedure Reference | CS 61A Fall 2020

https://cs61a.org/articles/scheme-builtins.html 15/17

(setposition )

Moves the turtle to position ( x , y ) without changing its heading.

Aliases: setpos , goto

showturtle

(showturtle)

Makes the turtle visible.

This procedure has no e�ect on the web interpreter, as the turtle is always invisible.

Alias: st

speed

(speed )

Sets the turtle’s animation speed to some value between 0 and 10 with 0 indicating no

animation and 1-10 indicating faster and faster movement.

On the local interpreter in tk-turtle mode, this changes the animation speed. This feature

has no e�ect on the web interpreter and on the gui-less pillow-turtle mode.

Additional Reading
Scheme Speci�cation (scheme-spec.html) – the core speci�cation of 61A Scheme

R5RS Speci�cation (http://www.schemers.org/Documents/Standards/R5RS/) – the full

Scheme speci�caton that 61A Scheme most closely resembles.

https://cs61a.org/articles/scheme-spec.html
http://www.schemers.org/Documents/Standards/R5RS/
Free Hand

Free Hand