Midterm #1
Test Form A
27 September 2000
E ::= E ``*'' F | F
F ::= F ``+'' G | G
G ::= ``--'' H | H
H ::= N | Id | ``('' E ``)''
F & ::= & F ``+'' G | F ``--'' G | G
then
fun g (0,h,y) = y |g (n,h,y) = h(g(n-1,h,y)) + 1.0
fun nfold(f,0,x) = x |nfold(f,n,x) = f(nfold(f,n-1,x))
fun s n = n+1; fun compose(f,g) = fn x => f(g(x)); fun twice(f,y) = f(f(y)); fun ctwice f y = f(f(y)); fun sum(g,0) = g(0) |sum(g,n) = g(n) + sum(g,n-1);
fun fact 0 = 1 local
|fact n = n * fact(n-1); fun tfact(0,k) = k
|tfact(n,k) = tfact(n-1,n*k)
in
fun fastfact n = tfact(n,1)
end;
We can prove fact(n) = fastfact(n) by applying mathematical
induction (over n) to what property:
fun petruchio(nil,nil) = true
|petruchio(x::xs,y::ys) = petruchio(xs,ys)
|petruchio(xs,ys) = false
What does this function do: