; CS 42 Homework 1, Problem 3 ; Author(s): [your name here] ; Time spent: [enter the time spent on this homework here] ; You should ALWAYS include the above information. ; Additional comments (optional): ; ; You may add your definitions to this file and load it as Racket code. ; 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. ; You should use this file as a template for your hw1pr4.rkt file and for ; all future Racket files. ; Please first download to your directory the tester.rkt file, so that ; the unit tests make sense. You may get it at: ; ; http://www.cs.hmc.edu/courses/2010/fall/cs42/tester.rkt ; ; 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. Racket, 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.rkt") ; Right now, we provide at least one unit test for every function. You should add ; some more of your own, to check yourself. You should leave your unit tests in ; the submitted file. We will be using some unit tests that you haven't seen ; yet in evaluating your solutions, and we may evaulate your unit tests. ; ; 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. Racket, 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 3.1 ; ; Function: erdos ; Input: ; N - a non-negative integer ; Output: ; Computes the Erdos number of the input: 3N+1 if N is odd; N/2 if N is even (define (erdos N) 0) ;; Replace with the correct definition ; Unit tests for erdos (test (erdos 3) 10) (test (erdos 10) 5) ; Problem 3.2 ; Function: erdosCount ; Input: ; N - a non-negative integer ; Output: ; The smallest number of times erdos must be iterated, when started with an ; input of N, in order to arrive at a value of 1 (define (erdos-count N) 0) ;; Replace with the correct definition (test (erdos-count 26) 10) (test (erdos-count 27) 111) ; Problem 3.3 ; ; Function: removeAll ; Input: ; e - an element ; L - a list ; Output: ; A list that is identical to L except with top-level instances of e removed. (define (removeAll e L) '()) ;; Replace with the correct definition (test (removeAll "i" '("a" "l" "i" "i" "i" "e" "n")) '("a" "l" "e" "n")) (test (removeAll "i" '( ("a" "l" "i") "i" "i" "e" "n" )) '(("a" "l" "i") "e" "n")) (test (removeAll 0 '(1 0 1 0 1 0)) '(1 1 1)) ; Problem 3.4 ; ; Function: find ; Input: ; L - A list ; M - A list that may or may not contain L as a sublist ; Output: ; #t if L is a sublist of M, #f otherwise (define (find L M) #f) (test (find '(1 2 3) '(0 1 2 3 4)) #t) (test (find '(1 2 3) '(0 1 2 2 3 4)) #f) ; And a few more (test (find '() '(1 2 3)) #t) (test (find '(1) '()) #f) (test (find '(1 2) '(2 1 1 1)) #f) (test (find '(1 2) '(2 1 1 (1 2))) #f) ;; an element is not a sub-list (test (find '(1 1 3) '(2 1 1 2 3 1)) #f) ; The following will give the overall number of tests correct and incorrect. (tester 'show)