Lec13 - CMPSC 461: Programming Language Concepts Lecture 13...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSC 461: Programming Language Concepts Lecture 13 Instructor: Prof. Swarat Chaudhuri
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Problem 1 (1! + 2! + … + n!) (define sumfact (lambda (n) (letrec ([fact (lambda (m) (if (zero? m) 1 (* m (fact (- m 1)))))]) (if (= n 1) 1 (+ (sumfact (- n 1)) (fact n))) )))
Background image of page 2
Problem 2 What does this function do? int foo (int n) { int c = 1, d = 1; for (c = 1; c <= n; c++) { d = d * c; c ++; } return d; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Problem 2: plain recursion If n is odd, returns (1 * 3 * 5 * … * n) If n is even, returns (1 * 3 * … * (n – 1)) (define (foo n) (if (odd? n) (if (= n 1) 1 (* n (foo (- n 2))) (foo (- n 1))))
Background image of page 4
Problem 2: tail recursion (define (prob-2-tr n) (letrec ([aux (lambda (c d) (if (> c n) d (aux (+ c 2) (* d c))))]) (aux 1 1)))
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Proving theorems Prove that (foo n) is odd for any n > 0. (define (foo n) (if (odd? n) (if (= n 1) 1 (* n (foo (- n 2))) (foo (- n 1))))
Background image of page 6
Prove that (foo n) is odd for any n > 0. Strong mathematical induction:
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

Lec13 - CMPSC 461: Programming Language Concepts Lecture 13...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online