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

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

CMPSC 461: Programming Language Concepts Lecture 13 Instructor: Prof. Swarat Chaudhuri

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

View Full Document
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))) )))
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; }

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

View Full Document
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))))
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)))

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

View Full Document
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))))
Prove that (foo n) is odd for any n > 0. Strong mathematical induction:

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online