wk3.2.html
procedure: (transcript-on filename)
returns: unspecified
filename must be a string.
transcript-on opens the file named by filename for
output, and it copies to this file all input from
the current input port and all output to the
current output port. An error is signaled if the
file cannot be opened for output.
procedure: (transcript-off)
returns: unspecified
transcript-off ends transcription and closes the
transcript file.
Variable Arity procedures in Scheme.
+, list, vector, ...
(lambda formal body)
Formal is a single variable
When the resulting procedure is invoked
this variable is bound to a LIST of the
argument values.
A procedure that behaves like a curried +
when invoked with one argument, otherwise
like +
(define plus
(lambda x
(if (null? (cdr x))
(lambda (y) (+ (car x) y))
(apply + x))))
Inductive specifications and recursive programming
1. 0 E S
2. whenever x E S, x + 3 E S
0 3 6 9 12 ...
Inductive specification of "multiples of 3"
In general:
1. Some specific value is in S
2. If certain values are in S,
then certain other values are in S
Inductive specification of data types:
"List of Numbers"
1. ()
2. if L is a "ListofNumbers" and n is a number,
then the pair (n.L) is a "ListofNumbers"
()
(14)
(3 14)
...
BNF for inductive specifications
- especially useful to specify programming language
syntax --> compilers.
- theory of computing
::== ()
::== ( . )
OR
::== ()
| ( . )
terminals, Syntactic categories
more Shortcuts:
Kleene Star
::== ({}*)
// 0 or more numbers
Kleene Plus
::== ({}+)
// 1 or more numbers
Prove that a given data value is syntactic
category X
Derivation:
(14.()) is a listofnumbers
=> (.)
=> (14.)
=> (14.())
Another way equally ok.
=> (.)
=> (.())
=> (14.())
Scheme example (language specification example):
using BNF
datum is an literal data representation
::== ({}*)
::== ({}+ . )
::== #({}*)
::== |
| |
| |
|
BNF rules are context free
programming languages are not
Prose accompanying BNF
provides context in language specification
Compilers use limited context senstive BNF rules
Recursive specification of programs
1. if n is 0, e(n,x) = 1
2. if n > 0, assume e(n-1,x) is known/well defined
then
e(n,x) = x * e(n-1,x)
(define e
(lambda (n x)
(if (zero? n)
1
(* x (e (- n 1) x)))))
When defining a program based on structural
induction, the structure of the program should
be patterned after the structure of the data.
(define listofnumbers?
::== ()
| ( . )
(define list-of-numbers?
(lambda (lst)
(if (null? lst)
#t
(if (pair? lst)
(if (number? (car lst))
(list-of-numbers? (cdr lst))
#f)
#f))))
nth-elt : takes a list lst
and a 0 based index n and returns
element n of lst
> (nth-elt '(a b c) 0)
a
;;
> (nth-elt '(a b c) 1)
b
> (nth-elt '(b c) 0)
b
;;
> (nth-elt '(a b c) 2)
c
> (nth-elt '(b c) 1)
c
> (nth-elt '(c) 0)
c
len: takes a list lst
and returns its length
> (len '(a b c d))
4
> (len '())
0
> (len '(a))
1
> (+ 1 (len '()))
1
> (len '(a b c))
3
> (+ 1 (len '(b c)))
3
> (+ 1 (+ 1 (len '(c))))
3
> (+ 1 (+ 1 (+ 1 (len '()))))
3
remove-first: takes two arguments
s symbol
los list of symbols
returns a list with same elements arranged
in the same order as los, except that the first
occurence of s in los is removed.
That is: removes the first occurence of s in los
> (remove-first 'a '(b a c))
(b c)
> (remove-first 'a '(a v c))
(v c)
remove: takes two arguments
s symbol
los list of symbols
returns a list with same elements arranged
in the same order as los, except that all
occurences of s in los are removed.
That is: removes ALL occurence of s in los
> (remove 'a4 '(c a4 d1 a4))
(c d1)
subst: takes three arguments
new symbol
old symbol
slst
Replaces all occurences of old by new
> (subst 'a 'b '((b c) (b d)))
((a c) (a d))
::== ({}*)
::== |
Sushil Louis
Last modified: Mon Sep 13 09:48:08 PDT 1999