; CS 60 Assignment 1 Functional Programming Warmup 100 points total ; ; Author: Use_your_own_name ; ; ALWAYS put your own name after the header. ; ; Due Sunday 1/24/2010 at 11:59 p.m. at the web submission site: ; ; http://www.cs.hmc.edu/~cs60grad/submissions/submit.py ; ; The first time you use Dr. Scheme, select the language "Pretty Big", which ; is what we will be using for the Scheme part of the course. ; ; Add your solution definitions to this file, load it as Scheme code, ; then submit it when you are happy with the results. You may submit multiple ; times. Only the last submission will be graded. ; ; Please first download to your directory the tester.scm file from the web page, ; so that the unit tests make sense. ; ; Put it in the same directory from which you load your file, so that it will be ; loaded whenever you load your file. This tester is used for all Scheme-based ; assignments. ; ; NOte: There is a built-in test facility in Dr. Scheme, but please ; DO NOT use it, as it will convert all of your source code into XML, ; making it impossible for anyone to read as text.) (load "tester.scm") ; When run as is, all tests will fail. By replacing the definitions ; with correct ones, you should get more tests succeeding, and eventually ; all tests should succeed. ; ; We provide at least one unit test for every function. You may wish to add ; some more tests of your own, to check yourself. You may leave your unit tests ; in the submitted file. We will be using a different unit test facility and ; will test your definitions on examples that you haven't seen. ; ; You do not have to worry about checking for malformed input. We will not be ; using any test cases such as that. ; ; You should pay attention to elegance, readability, and comments where needed. ; Formatting should be straightforward if you use Dr. Scheme, since it indents ; for you. ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; All answers MUST be done using functional programming. ; ; Do not use assignment! ; ; Do not use conventional iteration. It isn't needed. ; ; The emphasis here is on constructing and composing functions. Non-compliant ; ; solutions will be counted as wrong. If you don't know how to approach a ; ; problem in the prescribed way, please seek help at your earliest opportunity.; ; ; ; Include comments and use meaningful identifiers where possible. ; ; Make your solutions elegant and clear as possible. ; ; We are interested in style, rather than simply answers. ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Problem 1: [10 points] ; ; Heron's formula for the area of a triangle given its three sides is ; a beautiful little formula. You find its definition on the wikipedia ; http://en.wikipedia.org/wiki/Heron%27s_formula ; ; Devise a comparably beautiful Scheme function 'heron' with three arguments ; that computes the area based on this formula. Use a 'let' for the value ; of s so that you don't repeat sub-expressions. The built-in 'sqrt' can be ; used to extract the square root. (define (heron a b c) 0) ;; Replace with the correct definition ; Unit tests for heron (test (heron 3 4 5) 6) (test (heron 12 13 5) 30) (test (heron 42 29 29) 420) (test (heron 1e10 1e10 1) 5e9) (test (heron 1 1 1) (/ (sqrt 3) 4)) ; Problem 2: [20 points] ; ; Look up Zeller's congruence in the wikipedia. This is a formula for finding the day ; of the week of any day in the Gregorian calendar (the calendar we usually use) given the ; year, month, and date. Give a scheme definition of function 'zeller', wherein the ; year, month, and day are numbers, as indicated on the web page, and the returned value ; is the number of the day of a week, starting with 0 for Saturday, 1 for Sunday, etc. ; Note that the translation of "January and February are counted as months 13 and 14 ; of the previous year" is a little tricky. In effect, the previous year must be used when ; these months arise. This can be handled by over-riding year using a 'let'. (define (zeller year month day) 0) ;; Replace with the correct definition (test (zeller 1975 7 20) 1) (test (zeller 1991 5 1) 4) (test (zeller 2000 1 11) 3) ; Problem 3: [10 points] ; ; Define a function 'month-number that returns the number of a symbolic month, given the ; Scheme symbol for the month. The month symbols are to be 3-letter symbols, 'jan, 'feb, ... ; and the numbers run from 1 to 12. (Use the assoc built-in function.) (define (month-number month-name) 12) ;; Replace with the correct definition (test (month-number 'sep) 9) (test (month-number 'jul) 7) (test (month-number 'jan) 1) ; Problem 4: [10 points] ; ; Define a function 'day-name' that returns a 3-letter day symbol, given the number to ; which the day would correspond as the output of 'zeller'. (define (day-name day-number) 'fri) ;; Replace with the correct definition (test (day-name 3) 'tue) (test (day-name 0) 'sat) (test (day-name 1) 'sun) ; Problem 5: [10 points] ; ; Compose the functions in your previous definitions to give a more user-friendly ; version of 'zeller', which we call 'day-of-week'. This takes a year, ; a month symbol, and a day of the month and returns a day-of-the-week symbol. ; Maybe test it on your own birthday. (define (day-of-week year month-name day) 'fri) ;; Replace with the correct definition (test (day-of-week 1975 'jul 20) 'sun) (test (day-of-week 1979 'dec 2) 'sun) (test (day-of-week 2000 'jan 11) 'tue) ; Problem 6: [5 points] ; ; Given below is an association list that gives a Scrabble letter score for ; each of the 26 letters of the alphabet. Use this association list to create a ; function 'score-letter' that will return the score of its argument letter. ; Have your function return 0 if the argument is not a letter. (define scrabble-letter-scores '((#\a 1) (#\b 3) (#\c 3) (#\d 2) (#\e 1) (#\f 4) (#\g 2) (#\h 4) (#\i 1) (#\j 8) (#\k 5) (#\l 1) (#\m 3) (#\n 1) (#\o 1) (#\p 3) (#\q 10)(#\r 1) (#\s 1) (#\t 1) (#\u 1) (#\v 4) (#\w 4) (#\x 8) (#\y 4) (#\z 10) )) (define (score-letter letter) -1) ;; Replace with the correct definition (test (score-letter '#\w) 4) (test (score-letter '#\z) 10) (test (score-letter '_) 0) ; Problem 7: [10 points] ; ; Construct a function score-word that scores an entire word represented as a symbol. ; Use higher-order functions such as foldr and map to do any necessary iteration implicitly. ; Note that symbols are not the same as strings. However, the built-in function ; symbol->string will convert a symbol into a string of characters, and the built-in function ; string->list will convert a string into a list of its characters. (define (score-word word) 0) ;; Replace with the correct definition (test (score-word 'foo) 6) (test (score-word 'aeiou) 5) (test (score-word 'abcdefghijklmnopqrstuvwxyz) 87) (test (score-word 'bcdfghjklmnpqrstvwxyz) 82) ; Problem 8: [25 points] ; ; Construct a function that finds the highest-scoring word in a list of symbols, ; returning both the word and the score as a list of two elements. This may require ; the invention of some helper functions. For the case that the list of symbols is empty, ; return the following dummy value: '(_ 0) (define (highest-word-score list-of-words) '(foo 0)) ;; Replace with the correct definition (test (highest-word-score '(two words)) '(words 9)) (test (highest-word-score '(one_word)) '(one_word 11)) (test (highest-word-score '(the quick brown fox just plays with my very lazy dog)) '(quick 20)) (test (highest-word-score '(now is the time for each good person to come to the aid of his or her party)) '(party 10)) (test (highest-word-score '()) '(_ 0)) ; Extra Credit Problem 9: [25 points extra] ; ; Construct a function best-word that returns the highest Scrabble-scoring symbol ; among the symbols in the second argument ; that can be created by using each letter in the first argument at most once. ; If there is a tie, an arbitrary one of the symbols in the list with maximal ; score can be returned. (In grading your problems, we will make sure that the chosen word ; in each test case is unique.) Examples of successful unit tests are given after the stub definition. ; Notice that in the second example, two p’s of apple were used to form 'pape, while in the ; third example, 'pape could not be made because of too few p’s in 'pale. Try to give ; some attention to efficiency in your solution. For example, enumerating all permutations ; of the letters in the first argument would not be a good idea, because perhaps relatively ; few of those permutations occur in the second argument list. (define (best-word symbol list-of-words) '()) ; Uncomment these if you do the exta credit problem: ; ;(test (best-word 'academy '(ace ade cad cay day)) '(cay 8)) ; ;(test (best-word 'apple '(peal peel ape pape)) '(pape 8)) ; ;(test (best-word 'pale '(peal peel ape pape)) '(peal 6)) ; ; The following will give the overall number of tests correct and incorrect: (tester 'show)