;;; ======================================================= ;;; CMPU-101, Spring 2013 ;;; Assignment 4 ;;; DUE DATE: March 8th, by 8 pm ;;; ======================================================= (require 2htdp/image) (display "\n CS101 Assignment 4, Spring 2013") (display "\n PLEASE WRITE YOUR NAME HERE\n\n") ;; Fill in the function definitions within the boundaries ;; provided. Use the design recipe and follow the grading ;; criteria posted along with this assignment. ; ; Problem 1: ; ; A list of anything (LOX) is either: ; 1. empty, or ; 2. it's a (cons x lix), where x is anything and lix ; is an LOX one element smaller than (cons x lix). ; ; ; Write a function called LAST that consumes an LOX and ; returns the last element in the list. ; ; If the input list is empty, the function should return ; an error message: ; ; "Error in last: No elements in list." ; ; A note of caution: If the recursion goes all the way ; to the empty list, the same string will be returned ; for every list. Therefore, this function may require ; more than one base case so that it stops before becoming ; empty. ; (display "\n\n----------------------------\n") (display "Problem 1: LAST") (display "\n----------------------------\n") ;; Contract: ;; Header: ;; Purpose: ;;; Pre-function tests (given to help you understand problem): ;(check-expect (last (list "Tom" "Dick" "Harry")) "Harry") ;(check-expect (last (list 2 "four" 5 "six" 7 1)) 1) ;(check-expect (last empty) "Error in last: No elements in list.") ; ;;; Function definition: ;(define last ; ; ; ; Post-function printfs: (newline) (newline) ; ; Problem 2: ; ; A list of numbers (LON) is either: ; 1. empty, or ; 2. it's a (cons n lin), where n is a number and lin ; is an LON one element smaller than (cons n lin). ; ; ; Write a function called GENLIST-N that takes in a ; non-negative (i.e., 0 included) natural number n and ; returns a LON that contains all the numbers from 1 to n, ; in that order, from left to right. ; ; Write this function using a local accumulator function ; inside the GENLIST-N function so the initial value of ; the accumulator is hidden from the caller of the function. ; ; (display "\n\n----------------------------\n") (display "Problem 2: GENLIST-N") (display "\n----------------------------\n") ;; Contract: ;; Header: ;; Purpose: ;;; Pre-function tests (given to help you understand problem): ;(check-expect (genlist-n 3) (list 1 2 3)) ;(check-expect (genlist-n 6) (list 1 2 3 4 5 6)) ;(check-expect (genlist-n 0) empty) ; ;;; Function: ;(define genlist-n ;;; Post-function printfs: (newline) (newline) ; Problem 3: ; ; Write a function called filter-nums that consumes ; a LOX and produces a LON containing only the numbers ; in the input list. ; ; See data definitions for LOX and LON in problems ; 1 and 2. You do not need to repeat those definitions ; for this problem. ; (display "\n\n----------------------------\n") (display "Problem 3: FILTER-NUMS") (display "\n----------------------------\n") ;; Contract: ;; Header: ;; Purpose: ;; Pre-function tests (given to help you understand problem): ;(check-expect (filter-nums (list 42 "45" "joe" 0)) (list 42 0)) ;(check-expect (filter-nums (list "78" 'jack 'joe 'zero)) empty) ;(check-expect (filter-nums (list 0 -23 90 'hi)) (list 0 -23 90)) ;(check-expect (filter-nums empty) empty) ;; Function: ;(define filter-nums ;; Post-function printfs: (newline) (newline) ; ; Problem 4: ; ; Write a function called CONCENTRIC-RINGS that consumes ; a LON called lor. The list lor contains positive numbers ; that represent the radii of circles. The function should ; produce purple circles in outline mode with the radius ; of each circle equal to the number specified by (first lor). ; The circles should be displayed on an empty scene that is ; 400 X 400 pixels in area. ; ; Each circle should be centered in the same location, ; at (200,200). Use the place-image function to bind together ; all the images on one empty scene. ; ; See the data definition for list of numbers, LON, in ; problem 2. You do not need to repeat this definition for ; this problem. ; (display "\n\n----------------------------\n") (display "Problem 4: CONCENTRIC-RINGS") (display "\n----------------------------\n") ;For your convenience, the following constants are given: (define TENS (list 1 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200)) ;(define NUMS (genlist-n 200)) ;;; NOTE: NUMS is constructed using function from problem 2, ;;; so be sure that function is written before uncommenting ;;; the line defining NUMS above. ;; Constant definitions (use whichever ones you choose or make ;; your own): ;(define HEIGHT 400) ;; Height of scene ;(define WIDTH 400) ;; Width of scene ;(define MODE "outline") ;; Mode of circles ;(define COLOR "purple") ;; Color of circles ;(define MT (empty-scene WIDTH HEIGHT)) ;; empty scene ;(define CENTER-W (/ WIDTH 2)) ;; x coordinate of MT center ;(define CENTER-H (/ HEIGHT 2)) ;; y coordinate of MT center ;; Contract: ;; Header: ;; Purpose: ;; Pre-function tests: None possible because function produces ;; an image. Tests are by visual inspection. ;;; Function: ;(define concentric-rings ; ; ; ;;; Post-function printfs: (newline) (newline)