Scheme Quick Reference

Similar documents
Scheme Quick Reference

How to Design Programs Languages

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Essentials of Programming Languages Language

Essentials of Programming Languages Language

Documentation for LISP in BASIC

CSE 341 Section Handout #6 Cheat Sheet

CSC 533: Programming Languages. Spring 2015

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Introduction to Scheme

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Functional Programming Languages (FPL)

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Scheme as implemented by Racket

How to Design Programs Languages

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Languages

Programming Languages

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

User-defined Functions. Conditional Expressions in Scheme

LECTURE 16. Functional Programming

Chapter 15 Functional Programming Languages

CS 314 Principles of Programming Languages

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

A Brief Introduction to Scheme (II)

Evaluating Scheme Expressions

An Introduction to Scheme

Lexical Considerations

1 Lexical Considerations

Typed Racket: Racket with Static Types

Functional Programming - 2. Higher Order Functions

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CS61A Midterm 2 Review (v1.1)

Lexical Considerations

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

FP Foundations, Scheme

The Typed Racket Guide

ALISP interpreter in Awk

Project 2: Scheme Interpreter

IC Language Specification

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Vectors in Scheme. Data Structures in Scheme

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

The PCAT Programming Language Reference Manual

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

INTRODUCTION TO SCHEME

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

Organization of Programming Languages CS3200/5200N. Lecture 11

ECE570 Lecture 2: Types and Conventions

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

ECE473 Lecture 2: Types and Conventions

Programming Languages: Application and Interpretation

CS 314 Principles of Programming Languages

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Programming Languages: Application and Interpretation

Lecture Notes on Lisp A Brief Introduction

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Functional Programming. Big Picture. Design of Programming Languages

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Scheme: Expressions & Procedures

Chapter 15. Functional Programming Languages

CS 314 Principles of Programming Languages. Lecture 16

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Repetition Through Recursion

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Building a system for symbolic differentiation

CSCI337 Organisation of Programming Languages LISP

The SPL Programming Language Reference Manual

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

CSc 520 Principles of Programming Languages

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

An introduction to Scheme

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

CS450 - Structure of Higher Level Languages

Functional Programming

CSCC24 Functional Programming Scheme Part 2

Functional Programming Languages (FPL)

Functional programming with Common Lisp

Common Lisp. Blake McBride

Introductory Scheme. Revision 1

Revised 5 Report on the Algorithmic Language Scheme

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

1.3. Conditional expressions To express case distinctions like

The Typed Racket Reference

Scheme->C Index to the Report on the Algorithmic Language Scheme 15 March 1993

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Building a system for symbolic differentiation

Decaf Language Reference Manual

Sprite an animation manipulation language Language Reference Manual

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Administrivia. Simple data types

Transcription:

Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete language reference, but it describes some of the most common syntax and built-in procedures of the language. 1.1 Notation The following notational conventions are used in this document. Text denoting literal Scheme code is written in typewriter font. Parameters are indicated by writing their names in italics, subject to the following type conventions: f A function (arbitrary) g A generic function p A predicate (function returning #t or #f) n A number z An exact integer s An identifier (symbol) lst A proper list c A pair ( cons box ) w, x, y Arbitrary Scheme objects v A vector expr... Zero or more occurrences of expr expr 1 expr 2... One or more expressions [item] Indicates an optional item or items. Multiple variables of a given type will be indicated by giving them subscripts to distinguish one from the next. 2 Syntax 2.1 Identifiers An identifier consists of one or more letters, digits, and extended alphabetic characters, beginning with a character that cannot begin a number. In addition, +, -, and... by themselves are identifiers (even though these characters could otherwise begin numbers). The extended alphabetic characters include the following: 2.2 Types! % & * + -. / : < = >? $ ^ By convention, the names of identifiers that are bound to types are written in <angle-brackets>. The following table gives a subset of the class hierarchy provided by default in Swindle. <obj> <object> <class> <procedure-class> <primitive-class> <generic> <method> 1

<function> <generic> <procedure> <primitive> <sequence> <improper-list> <pair> <list> <nonempty-list> <null> <vector> <string> <symbol> <boolean> <char> <number> <exact> <inexact> <complex> <exact-complex> <inexact-complex> <real> <exact-real> <inexact-real> <rational> <exact-rational> <inexact-rational> <integer> <exact-integer> <end-of-file> <port> <input-port> <output-port> <macro> <void> <exn> <promise> <procedure> <inexact-integer> (also under <pair>) 2.3 Booleans The expression #t means true, and #f means false. In addition, any Scheme object other than #f is considered true with respect to conditional forms such as if or cond. 2.4 Lists The expression () denotes the empty list, or null. A proper list is either the empty list, or a <pair> whose second element (cdr) is a proper list. 3 Special Forms Each special form has its own evaluation rule. Some of the most common special forms are given here, along with a brief synopsis of the evaluation rule for each. (define s expr) Defines the identifier s to have the value returned by the evaluation of expr. (begin expr 1 expr 2...) Evaluates expressions from left to right and returns the value of the last. (quote expr) Returns expr unevaluated. Equivalently, this can be written expr. 2

(lambda (s 1...) expr 1 expr 2...) Creates a new procedure whose formal parameters are s 1, s 2,... and whose body is the given list of expressions. (if test expr 1 expr 2 ) Evaluates test, which is an arbitrary Scheme expression. If the resulting value is #f, expr 2 is evaluated; otherwise expr 1 is evaluated. (cond (test 1 elst 1 ) (test 2 elst 2 )... [(else elst e )]) Evaluates each test expression in left-to-right order. If test i evaluates to a true value, then the corresponding list of expressions elst i is evaluated, and the value of the last expression is returned. The optional else clause is evaluated if none of the preceding tests yields a true value. (and expr...) Evaluates expressions from left to right, and returns the value of the first expression that returns #f. If no expression returns #f, the value of the last expression is returned, or #t if there are no expressions. (or expr...) Evaluates expressions from left to right, and returns the value of the first expression that returns a true value (i.e., not #f). If there are no expressions, or all expressions return #f, then the or returns #f. (let ((s 1 texpr 1 ) (s 2 texpr 2 )...) expr 1 expr 2...) Evaluate expr 1, expr 2,... in an environment with s 1 bound to the value of texpr 1, s 2 bound to the value of texpr 2, etc. (let* ((s 1 texpr 1 ) (s 2 texpr 2 )...) expr 1 expr 2...) As let, except that each texpr is evaluated in an environment where all previous bindings (those further to the left) are visible. (letrec ((s 1 texpr 1 ) (s 2 texpr 2 )...) expr 1 expr 2...) As let, except that each texpr is evaluated in an environment where all the variables s 1, s 2,... are visible. This permits definition of recursive procedures. (set! s expr) Change the value of the lexically closest binding of s to the value resulting from evaluation of expr. Note: You may not use this form in your code unless the instructions explicitly say so. (define-class s (supers) (slot type [init])...) Define a new class with the specified superclasses (zero or more) and slots (zero or more), and bind it to the name s in the global environment. The init expressions are optional, but if present, will provide default values for their slot. (make class : key 1 expr 1...) Create a new instance of class with the specified initial values for its slots. If class has a slot named s, the corresponding keyword is :s. Keyword arguments may be specified in any order. (defgeneric s (params)) Creates a new generic function named s which takes as many parameters as are specified in params. (add-method g f) Add a method to the specified generic g. It is necessary that f be a function with the same number of parameters as g is defined to take. 4 Standard Procedures All the forms given here are standard procedures; that is, they are evaluated according to the standard Scheme rule, and all their arguments are evaluated before the procedure is applied. 4.1 Equality Testing (eq? x y) Returns #t if x and y should be regarded as the same object, #f otherwise. Two symbols are eq? if they are spelled the same (case does not matter), the empty list () is eq? to itself, and any pair is eq? to itself. (equal? x y) Returns #t if x and y print the same, #f otherwise. In Swindle, equal? is a generic function, and you can add new methods to change the behaviour of the equal? relationship. (= n 1 n 2...) Returns #t if n 1 = n 2 =.... Note that this works only for numbers. (zero? n) Returns #t if n = 0. Note that this works only for numbers. 3

4.2 List Structure Operations (null? x) Returns #t if x is the empty list (), #f for any other Scheme object. (cons x y) Creates a new pair (of type <pair>) whose car is x and whose cdr is y. (car c) Returns the first element of c. (cdr c) Returns the second element of c. (cadr c) (caddr c) (cddr c) (cdar c) These are defined as compositions of car and cdr. For instance, (cadr c) is equivalent to (car (cdr c)), and (cddr c) is equivalent to (cdr (cdr c)). All combinations of up to 4 a s and d s are defined for you by Scheme. (set-car! (set-cdr! c expr) Set the car of c to the value of expr. Do not use unless explicitly permitted to do so. c expr) Set the cdr of c to the value of expr. Do not use unless explicitly permitted to do so. (list expr 1 expr 2...) Create a new list consisting of the values of the given expressions. (list-ref lst z) Return the element at index z of the given list, where 0 is the index of the first element in a list. It is an error if z < 0 or z is past the end of the list. (length lst) Return the number of elements in the given list. (append lst 1 lst 2 ) Append the two lists together, and return the resulting list. (reverse lst) Return a new list which has same elements as lst, but in the opposite order. (member x lst) Return the first sublist of lst whose car is x, or #f if x does not occur in the list. Uses equal? for comparison. (memq x lst) As member, but uses eq? for comparision. 4.3 Arithmetic and Numeric Operators (= n 1 n 2...) (< n 1 n 2...) (> n 1 n 2...) (<= n 1 n 2...) (>= n 1 n 2...) Tests whether a sequence of numerical values are, respectively, equal, strictly increasing, strictly decreasing, non-decreasing, or non-increasing. (+ n 1 n 2...) Addition. (add1 n) Adds 1 to any number. (- n 1 n 2...) Subtraction (negation, with a single argument). (sub1 n) Subtracts 1 from any number. (* n 1 n 2...) Multiplication. (/ n 1 n 2...) Real or rational division (associates to the right). (quotient n 1 n 2...) Quotient from integer division (remainder n 1 n 2...) Remainder from integer division (modulo n 1 n 2...) Least non-negative residue of remainder. (abs n) Absolute value. (min n 1 n 2 ) (max n 1 n 2 ) Returns the smaller (larger) value of n 1 and n 2. (sqrt n) Square root. (expt n 1 n 2 ) Computes n n2 1. 0x = 1 if x = 0, 0 otherwise. (exp n) Computes e n. (log n) Computes ln(n). (gcd z...) Returns the greatest common divisor of its arguments (0 if none). (floor n) Returns the largest integer not greater than n. (ceiling n) Returns the smallest integer not less than n. (sin n) (cos n) (tan n) Standard trigonometric functions. Angles in radians. (atan n) Arc tangent (radians). 4

4.4 Higher-Order Procedures (apply f args) Call the function f with the specified arguments, as if you had written (f arg 1 arg 2...) (map f lst 1 lst 2...) Apply f to each element of all the input lists, in some order, and collect the return values into a list, which is returned from map. (filter p lst) Returns a list consisting of the elements from lst for which p returns a true value. (foldr f x lst) Uses f to combine all the elements of lst in right-to-left order, using x as the starting value. Returns the resulting value. The foldl function does the same, except it processes the list in left-to-right order. 4.5 Vector Operations (vector expr 1 expr 2...) Creates a new vector containing the values of the given expressions. (make-vector z [expr]) Creates a new vector of z elements, each initialized to the value of the given expr, if provided (otherwise, values are initially undefined). (vector-ref v z) Returns the element at position z of vector v, where 0 is the index of the first element of the vector. It is an error if z < 0 or z is past the last element in the vector. (vector-set! v z expr) Set the value at position z of vector v to the value of the expr. Vectors have fixed length, so it is an error to set an element outside the created range of the vector. 4.6 Stream Operations null-stream An object representing the empty stream. (null-stream? x) Returns #t if x is the empty stream, otherwise #f. (cons-stream x stream) Creates a new stream whose first element is x and the rest of which is a promise to evaluate stream. (stream-car stream) Returns the first element of stream. (stream-cdr stream) Returns the rest of stream, forcing evaluation of the promise. (map-stream f stream) Returns a new stream which is the result of applying function f to each element of stream. (filter-stream f stream) Returns a new stream which contains all the elements of stream for which f returns a true value. (map2-streams f stream 1 stream 2 ) Returns a new stream which is the result of applying f, a function taking 2 arguments, to the corresponding elements of stream 1 and stream 2. (stream->list stream z) Returns a list consisting of (up to) the first z elements of stream. (accumulate-stream f x stream) Uses f to accumulate all the elements of stream, using x as the starting element. Does not delay evaluation of the given stream. (integers-from z) Returns a stream of the integers in ascending order, starting with z. 4.7 Miscellaneous (not expr) Returns #t if expr is #f, otherwise returns #f. (pair? expr) Returns #t if expr is a pair, otherwise returns #f. (number? expr) (complex? expr) (real? expr) (rational? expr) (integer? expr) Returns #t if expr is, respectively, a number, a complex number, a real number, a rational number, or an integer; #f otherwise. Each numeric type is a subset of the previous numeric types in the list. (display expr) Writes a printable representation of expr to the default output port. (newline) Writes a newline character to the default output port. 5