Code Examples
This page (which you can read on the web at $PUB/code-examples.shtml) gives access to code examples in the course library and in the lecture notes directory.
You can use the table of contents (on the web) to jump directly to the kind of example you are looking for.
If you click on a Scheme file name and your browser tries to download a plugin (e.g., for a Lotus Screen Cam Movie), then you need to change your browser's rules for associating file suffixes with viewers. One way to do this on Windows (95/98) is to go into the Windows Explorer, and click on "View" then "Folder Options", then click on the "File Types" tab, and then enter a new file type for .scm files.
Contents
- Little Schemer, Chapter 2
- Little Schemer, Chapter 3
- Little Schemer, Chapter 4
- Scheme Background
- EOPL 2e Chapter 1
- EOPL 2e Chapter 2
- EOPL 2e Chapter 3
- EOPL 2e Chapter 4
- EOPL 2e Chapter 5
- EOPL 2e Chapter 6
The Little Schemer, Chapter 2
- Recursion over flat lists, without conditionals
$PUB/lib342/copy.scm, a prototype for these kinds of procedures, although it doesn't seem to do much.
$PUB/lib342/make-all-positive.scm
The Little Schemer, Chapter 3
- Recursion over flat lists, with conditionals
The Little Schemer, Chapter 4
$PUB/lib342/unary-notation.scm
Scheme Background
Data Types
Write Scheme code to:
- Construct and lists and improper lists
- Use of quote, cons, and list primitives (and when to use each)
- Extract parts of lists and improper lists
$PUB/lectures/basics/yodaize.scm
$PUB/lib342/lambda*.scm, for example $PUB/lib342/lambda-1-exp.scm
Understand:
- Dot notation
- How to read and write our type notation ($PUB/docs/typedscm.html)
In $PUB/lib342/ most files have type annotations (try the Unix command: grep deftype *.scm)
Expressions
Write Scheme code using expressions, statements, and definitions.
- Make decisions using if expressions
- Make declarations using define
$PUB/lib342/nth-element.scm, among many other examples in $PUB/lib.
Understand:
- Differences between expressions, statements, and declarations.
Procedures
- Use of map
$PUB/lib342/sym-exp.scm (parse-s-list)
$PUB/lib342/remove-sym-exp-with-map.scm (remove-s-list)
- Uses of curried procedures
$PUB/lib342/test-homework.scm (cs342:test-maker, cs324:eval-print-maker, cs342:regression-test-maker-maker, ...)
$PUB/lib342/tc-type-helpers.scm (tc:axiom-helper, tc:rule-helper, etc.)
$PUB/lib342/vector-generator.scm (see $PUB/lib342/vector-generator.tst and vector-map.scm for how it's used)
- Write variable arity procedures
$PUB/lib342/atomic-tree.scm (tree)
$PUB/lib342/set-ops-as-vector.scm (set-of, set-union*)
Syntactic Abstraction
- How to desugar expressions in Scheme (let, and, or, cond, case)
- Be able to use let, letrec, and, or, cond, case in Scheme programs. There are lots of examples of this. You can grep for the appropriate keyword to find examples in the library files. The following are some simple examples.
- examples of "and" and "or"
$PUB/lib342/set-ops-as-vector.scm (set-member?, set-subset?, set-equal?)
$PUB/lib342/tc-type-helpers.scm (tc:rule-or-helper, tc:sequence)
- examples of "let", "let*", "letrec", and "cond", e.g.,
$PUB/lib342/tc-type-helpers.scm (tc:sequence, ...)
EOPL Chapter 1
Inductive Specifications and Grammars (1.1)
- BNF notation
$PUB/lib342/lambda*.scm, for example $PUB/lib342/lambda-1-exp.scm
The document defining our type notation, which uses BNF.
$PUB/lib342/tc-output-type-expr.scm
Deriving Recursive Programs from Grammars (1.2)
See also the Following the Grammar handout.
- How a grammar relates to the outline of a recursive procedure (1.2.1)
See the Following the Grammar handout.
- Write recursive programs over flat lists (1.2.2)
$PUB/lib342/sum.scm (sum-of-list)
$PUB/lib342/product.scm (product-of-list)
$PUB/lib342/tc-util.scm (tc:last-item, tc:some, tc:member?, tc:filter, tc:find-duplicates, ...)
$PUB/lib342/type-predicates.scm (list-of)
- Write recursive programs over other grammars (1.2.2)
$PUB/lib342/select-outerwear.scm, which is an example using the temperature grammar (only alternatives, no recursion).
$PUB/lib342/iseq-map.scm, which is an example using the infinite sequence grammar (only recursion, no alternatives).
$PUB/lib342/valid-number.scm, which is an example using the phone number grammar (multiple nonterminals, no recursion).
$PUB/lib342/negate-bexp.scm, which is an example using the boolean expression grammar.
$PUB/lib342/targets.scm, which is an example using the statement-expression grammar.
$PUB/lib342/remove-named.scm, which is an example using the SXML grammar (an abstract syntax for XML).
$PUB/lib342/lambda-1-exp-examples.scm
$PUB/lib342/lambda-1-quote-exp-examples.scm
$PUB/lib342/lambda-if-exp-examples.scm
$PUB/lib342/tc-output-type-expr.scm
$PUB/lib342/tc-type-translate.scm (tc:type-vars, tc:output-type-vars, tc:nest-args, tc:type-unnest-args, tc:substq-all-output)
- Write recursive programs over symbol-expression and lists of symbol-expressions (s-lists, pp. 19-22)
$PUB/lectures/induction-recursion/count-sym-exp.scm
$PUB/lib342/remove-sym-exp.scm
try these using $PUB/lib342/sym-exp-cooked.scm, also instead of $PUB/lib342/sym-exp.scm
Tail Recursion (1.2.3)
- Understand the difference between full recursion and tail recursion.
Examples of full recursion are found in the section above on deriving recursive programs from grammars. Compare the code for the fully recursive $PUB/lib342/product.scm with the tail recursive code in $PUB/lib342/product-tail-recursive.scm. Note the pending computations surrounding the recursive calls in the fully-recursive version.
- Write tail-recursive procedures:
$PUB/lib342/product-tail-recursive.scm
$PUB/lib342/list-index-find.scm
$PUB/lectures/induction-recursion/vector-product.scm
$PUB/lib342/tc-scheme-parser.scm (tc:get-file-suffix and tc:last-dot-in-file-pos)
$PUB/lib342/tc-util.scm (tc:last-item)
$PUB/lib342/vector-map-bang.scm
- Using letrec
$PUB/lib342/whos-on-first.scm (get-context, try-strong-cues, try-weak-cues)
$PUB/lib342/set-ops-as-vector.scm
$PUB/lib342/tc-util.scm (tc:find-duplicates)
$PUB/lib342/vector-generator.scm
$PUB/lib342/type-predicates.scm (vector-of)
Scope and Binding of Variables (1.3)
- free and bound names and variable references in an expression
$PUB/lib342/lambda-1-exp-examples.scm (free-vars, free?, bound-vars, bound?)
$PUB/lib342/combinator-tools.scm (occurs-free-in?)
- combinators
see also $PUB/lib342/combinator-tools.scm
EOPL Chapter 2
Specifying Data via Interfaces (2.1)
Abstraction for Inductive Data Types (2.2)
- Use of define-datatype to declare variant record types
$PUB/lib342/lambda-1-exp-as-ast.scm
$PUB/lib342/lambda-1-quote-exp-as-ast.scm
- Abstract syntax that corresponds to a concrete syntax.
$PUB/lib342/lambda-1-exp-as-ast.scm
$PUB/lib342/lambda-1-quote-exp-as-ast.scm
The abstract syntax in chapter 3 of the EOPL text is a classic example of this.
- Use of cases in recursive programming, especially over some abstract syntax.
$PUB/lib342/arith-expr-examples.scm
$PUB/lib342/advice-table.scm (add-advice!)
$PUB/lib342/tc-type-translate.scm (tc:type-vars, tc:external-type-vars, tc:nest-args, tc:type-unnest-args, tc:substq-all-external)
$PUB/lib342/combinator-tools.scm (toCombinators, interpret, toLambda, occurs-free-in?)
Data Abstraction (2.3)
- Representation types and defrep
$PUB/lib342/set-ops-as-vector.scm
$PUB/lib342/statement-expression.scm
Transformation of Procedural to Abstract Syntax Tree Representations (2.3.2-2.3.3)
$PUB/lib342/seq-as-proc.scm transformed to $PUB/lib342/seq-as-ast.scm
$PUB/lib342/environment-as-proc.scm transformed to $PUB/lib342/environment-as-ast.scm
$PUB/lib342/phone-book-as-proc.scm transformed to $PUB/lib342/phone-book-as-ast.scm and optimized to a ribcage representation in $PUB/lib342/phone-book-as-ribcage.scm
$PUB/lib342/road-map-as-proc.scm transformed to $PUB/lib342/road-map-as-ast.scm
EOPL Chapter 3
Simple Interpreter (3.1-3.2)
$PUB/lib342/3-1-modified-in-class.scm
Conditionals (3.3)
$PUB/lib342/3-1-modified-in-class.scm
Local Binding (3.4)
$PUB/lib342/3-4-modified-in-class.scm
Procedures (3.5)
$PUB/lib342/3-5-modified-in-class.scm
Recursion (3.6)
Variable Assignment (3.7)
$PUB/lib342/3-7-modified-in-class.scm
Last modified Thursday, August 4, 2005.
This web page is for the Spring 2005 offering of Com S 342 at Iowa State University. The details of this course are subject to change as experience dictates. You will be informed of any changes. Thanks to Curtis Clifton for help with these web pages. Please direct any comments or questions to Gary T. Leavens.