# hw2 - Sheet1 Page 1 CS 61AWeek 2 Homework Solutions...

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

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

View Full Document

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.

Unformatted text preview: Sheet1 Page 1 CS 61AWeek 2 Homework Solutions Exercise 1.31(a): (define (product term a next b) (if (> a b) 1 (* (term a) (product term (next a) next b)))) (define (product term a next b) (define (iter a result) (if (> a b) result (iter (next a) (* result (term a))))) (iter a 1)) (define (! n) (product (lambda (x) x) 1 1+ n)) (define (pi terms) (* 4 (product (lambda (x) (/ (* 2 (1+ (floor (/ x 2)))) (1+ (* 2 (ceiling (/ x 2)))))) 1 1+ terms))) (define (pi terms) (* 4 (product (lambda (x) (/ (* (-1+ x) (1+ x)) (* x x) )) 3 (lambda (x) (+ x 2)) terms ))) Sheet1 Page 2 Exercise 1.32(a): (define (accumulate combiner null-value term a next b) (if (> a b) null-value (combiner (term a) (accumulate combiner null-value term (next a) next b)))) (define (accumulate combiner null-value term a next b) (define (iter a result) (if (> a b) result (iter (next a) (combiner (term a) result)))) (iter a null-value)) (define (sum term a next b) (accumulate + 0 term a next b)) (define (product term a next b) (accumulate * 1 term a next b)) Exercise 1.33: (define (filtered-accumulate combiner null-value term a next b predicate) (cond ((> a b) null-value) ((predicate a) (combiner (term a) (filtered-accumulate combiner null-value term (next a) next b predicate))) (else (filtered-accumulate combiner null-value term (next a) Sheet1 Page 3 next b predicate)))) (define (filtered-accumulate combiner null-value term a next b predicate) (define (iter a result) (cond ((> a b) result) ((predicate a) (iter (next a) (combiner (term a) result))) (else (iter (next a) result)))) (iter a null-value)) (define (sum-sq-prime a b) (define (square x) (* x x)) (filtered-accumulate + 0 square a 1+ b prime?)) (define (prod-of-some-numbers n) (filtered-accumulate * 1 (lambda (x) x) 1 1+ n (lambda (x) (= 1 (gcd x n))))) Exercise 1.40: (define (cubic a b c) (lambda (x) (+ (* x x x) (* a x x) (* b x) c))) Exercise 1.41: (define (double f) (lambda (x) (f (f x)))) Why does (((double (double double)) inc) 5) return 21 and not 13? The crucial point is that DOUBLE is not associative. > (((double (double double)) inc) 5) 21 > ((double (double (double inc))) 5) 13 DOUBLE turns a function into one that applies the function twice. Sheet1 Page 4 (DOUBLE DOUBLE) turns a function into one that applies the function four times....
View Full Document

## This note was uploaded on 09/14/2009 for the course PEIS 100 taught by Professor Mckenzie during the Spring '08 term at Berkeley.

### Page1 / 21

hw2 - Sheet1 Page 1 CS 61AWeek 2 Homework Solutions...

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

View Full Document
Ask a homework question - tutors are online
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.coursehero.com/file/5578827/hw2/", "fetch_time": 1493342303000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2017-17/segments/1492917122720.81/warc/CC-MAIN-20170423031202-00425-ip-10-145-167-34.ec2.internal.warc.gz", "warc_record_offset": 874014459, "warc_record_length": 23934, "token_count": 937, "char_count": 3104, "score": 4.03125, "int_score": 4, "crawl": "CC-MAIN-2017-17", "snapshot_type": "longest", "language": "en", "language_score": 0.6558423638343811, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00035-of-00064.parquet"}