a05sol - AssignmentS Solution Winter12 ;; Question 1 : [a]...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: AssignmentS Solution Winter12 ;; Question 1 : [a] ;; OddNat is one of: ;; * 1 ;; * [+ 2 OddNat] :[b] ;; odd-nat-fn: OddNat -> Any ;; [define [odd-nat-fn on] ;; [cond ' ii [[= 1 011] ---l \ I \ ;; [else [odd-nat-fn [- 2 011]] ...]]] :[C] ( ;; taylor-term: Num OddNat -> Num x ;; consumes a number x and an odd naturalfiunber n, and ;; produces the term of the Taylor su'mtha con ins xn. [check-expect [taylor-term -? 1h2] \ [check-expect [taylor-term .1] 0] ' [check-expect [taylor-terrV 3 3](/ [can 3 3] -6]] [define [taylor-term x11] / [cond [[= 11 1] X] y!) [else [/ [* -1 [sqr x] [ta or-term x [- 11 2D] [* n [sub1 11]]]]]] [check-expect [taylor-term -2 7] [/ [expt -2 7] 6040]] [check-expect [taylor-term 0 7] 0] [check-expect [taylor-term 3 9] [/ [expt 3 9] 362880)] :[d] ;; my-sin: Num OddNat -> Num ;; consumes a number x and an odd natural number n, and ; ; produces the term of the Taylor sum that contains xn [check-expect [my-sin 0 1] 0] [check-within [my-sin -2 101] [sin -2] 0.00001] This document is for the exclusive use of x8wen. [define [my-sin x n] [cond [[= 11 1] X] [else [+ [taylor-term x n] [my—8111 X [- 11 21111)] [check-expect [my-sin -2 1] -2] [check-within [my-sin 3 103] [sin 3] 0.00001] [check-within [my-sin 3 105] [sin 3] 0.00001] ;;Question 2 [require "pages.rkt"] ; ; A Page: [listof String] ii A Book: [listof Page] I \ ;; a] .fi ;; clean-book : Book -> Book ;; Removes punctuation and downcases the 'ven oo ;; Examples: " [check-expect [clean-book [list- ist”A‘bQ”] list --""c's"]]] [check-expect [clean-book empty] empty K“ [list [list "abc"] [list rG's”) ] _ [cond [[empty? Pages] empty} / [else [cons [clean-page [f'rst pages]] [clean-book [rest pages]]]]]] [define [clean-book pages? > [check-expect [clean-book [list [list "very," "messed." "up!" "-" "[sentence] this: one\"" "is?" "indeed;"]]] [list [list "very" "messed" "up sentence" "this" "one" "is" "indeed"]]] ;; clean-page : Page -> Page ;; Removes punctuation and downcases the given page. [check-expect [clean-page [list "asa,u]"]] [list "asau"]] [define [clean-page words] [cond [[empty? words] empty] [[contains-letters? [string->list [first words]]] [cons [list->string [clean-word [string->list [first words]]]] This document is for the exclusive use of x8wen. [clean-page [rest words]]]] [else [clean-page [rest words]]]]] [check-expect [clean-page [list water-loo”]] [list "water-loo"]] ;; contains-letters? : [listof char] -> Boolean ; ; Answers true iff the given list of characters contains at least one letter. [check-expect [contains-letters? [list #\a #\b]] true] [check-expect [contains-letters? [list #\. #\!]] false] [define [contains-letters? aloc] [cond [[empty? aloc] false] [else [or [char-alphabetic? [first aloc]] [contains-letters? [rest aloc]]]]]] [check-expect [contains-letters? [list #\u #\. #\w]] true . \ ;; clean-word : [listof char] -> [listof char] R ;; Removes punctuation and downcases the give1 list char cters [check-expect [clean-word [list #\w #\a #\ #\ it # JD #\0)] [list #\w #\a #\t #\l #\o #\o]] R xx “a [define [clean-word aloc] [cond [[empty? aloc] empty] [[or [char=? [first aloc] #\.] - [char=? [first aloc] #‘ ,] [char=? [first aloc] #\\0 .. [char=? [first aloc] #\ ] [char=? [first aloc] #\:_ / [char=? [first aloc] #\' [char=? [first aloc] #\"] [char=? [first aloc] #\?] [char=? [first aloc] #\!]] [clean-word [rest aloc]]] [else [cons [char-downcase [first alocj] [clean-word [rest aloc]]]]]] [check-expect [clean-word [list #\, #\.]] empty] [check-expect [clean-word [list #\S #\e #\e #\U #\!]] [list #\s #\e #\e #\u]] ;; b] ;; insert: [listof Nat] -> [listof Nat] ;; Purpose: produce an ascending list of page numbers by merging ,, a sorted list with a new element. [check-expect [insert [list 2 3 4 6] 5] [list 2 3 4 5 6]] This document is for the exclusive use of x8wen. [define [insert listofpages pagenum] [cond [[empty? listofpages] [list pagenum]] [[> pagenum [first listofpages]] [cons [first listofpages] [insert [rest listofpages] pagenum]]] [else [cons pagenum listofpages]]]] [check-expect [insert empty 1] [list 1]] [check-expect [insert [list 1 2 4 5] 3] [list 1 2 3 4 5]] ;; Index is one of: ;; * empty ;; * [cons [list String [listof Nat]] Index] ;; update-index : Index String Nat -> Index . ;; Updates the given index with the given word occurrih 011 the 'ven page. ;; Examples: R [check-expect [update-index empty "a" 1] [list [1' t "a [list DJ] [check-expect [update-index [list [list "a" [li t 2] ] ” ' 1_ [list [list "a" [list 1 2]]]] - [cond [[empty? index] " \ [list [list str [list pagenum] ]] - I [[string=? str [first [first [\ndexfi] [define [update-index index str pagenumg‘ [cond .. [[member? pagenum [second first index]]] index] [else [cons [list [first [fir ti dex]] [insert [second [fijst index]] pagenumD [rest index]]]]] [else [cons [first index] [update-index [rest index] str page11um]]]]] [check-expect [update-index [list [list "a" [list 1 2]]] "b" 1] [list [list "a" [list 1 2]] [list "b" [list 1]]]] [check-expect [update-index [list [list "a" [list 1 2]] [list "b" [list 3]]] "b" 1] [list [list "a" [list 1 2]] [list "b" [list 1 3]]]] ;; C] ;; add-page : Page Nat Index -> Index ; ; Adds the words in the given page [with the given page number] ; ; to the given index. This document is for the exclusive use of x8wen. ;; Examples: [check-expect [add-page [list "a" "b"] 2 empty] [list [list "b" [list 2]] [list "a" [list 2]]]] [check-expect [add-page [list "a" "b"] 2 [list [list "a" [list 2]] [list "b" [list 3]]]] [list [list "a" [list 2]] [list "b" [list 2 3]]]] [define [add-page words pnum index] [cond [[empty? words] index] [else [update-index [add-page [rest words] pnum index] [first words] p11um]]]] [check-expect [add-page . [list "the" "quick"] \ . \ 5 [list [list "the" [list 5 6 7]] .‘H [list "quick" [list 6 7 8]]]] [list [list "the" [list 5 6 7]] ( ) [list "quick" [list 5 6 7 8]]]] [check-expect [add-page [list "quickily"] .m- 5 "NR-\- [list [list "the" [list 5C6 7h . \ [list "quick" [list 67 DJ] [list [list "the" [list 5 6 > [list "quick" [lisli 6 7 8]] .. [list "quickily" [list 59]] [check-expect [add-page empty ) 5 [list [list "the" [list 5 6 7]] [list "quick" [list 6 7 8]]]] [list [list "the" [list 5 6 7]] [list "quick" [list 6 7 8]]]] [check-expect [add-page [list "the" "the"] 4 [list [list "the" [list 5 6 7]] [list "quick" [list 6 7 8]]]] [list [list "the" [list 4 5 6 7]] [list "quick" [list 6 7 8]]]] ;; d] This document is for the exclusive use of x8wen. ;; build-index: Book -> Index ;; Builds an index from a book. [check-expect [build-index empty] empty] [check-expect [build-index [list [list "The" "cat"] [list "in" "the" "hat!"]]] [list [list "hat" [list 2]] [list "the" [list 1 2]] [list "in" [list 2]] [list "cat" [list 1]]]] [define [build-index pages] [build-index-clean [clean-book pages]]] ;; build-index-clean: Book -> Index ;; Builds an index from a [presumed clean] book. [define [build-index-clean pages] [build-index-help pages 1]] . \ ;; build-index-llelp : Book Nat -> Index ;; Builds an index from the given [presumed Clea?) k sta ing at the given ;; page number. [define [build-index-help pages pnum] [cond [Iempty? pages] empty] " [else [add-page [first pages] pnum .i [build-index-help&ref pafis] [add1 pnum]]]]]] \ ) / This document is for the exclusive use of x8wen. ...
View Full Document

Page1 / 6

a05sol - AssignmentS Solution Winter12 ;; Question 1 : [a]...

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

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