;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname week4_tu_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "htdp"))))) ; a list-of-numbers is: ; empty, or ; (cons number list-of-numbers) ; template: ; (define (fun-for-list-of-numbers numlist) ; (cond [(empty? numlist) ...] ; [(cons? numlist) ... (first numlist) ... (fun-for-list-of-numbers (rest numlist)) ... ]) ; Q1: write the following function: ; double-list: list-of-numbers -> list-of-numbers ; inserts a duplicate of each list element x immediately following x (define (double-list numlist) (cond [(empty? numlist) empty] [(cons? numlist) (append (list (first numlist) (first numlist)) (double-list (rest numlist)))]) ) (check-expect (double-list (list 1 2 3)) (list 1 1 2 2 3 3)) (check-expect (double-list empty) empty) (check-expect (double-list (list 1 1)) (list 1 1 1 1)) ; Q2: write the following function: ; count-doubles: list-of-numbers -> number ; returns the number of doubled elements in the list ; first try: (define (count-doubles-old numlist) (cond [(empty? numlist) 0] [(cons? numlist) (cond [(= (first numlist) (first (rest numlist))) (+ 1 (count-doubles (rest numlist)))] [else (count-doubles (rest numlist))] )]) ) ; second try: (define (count-doubles numlist) (cond [(empty? numlist) 0] [(empty? (rest numlist)) 0] [else (cond [(= (first numlist) (first (rest numlist))) (+ 1 (count-doubles (rest numlist)))] [else (count-doubles (rest numlist))] )]) ) (check-expect (count-doubles (list 1 1 1)) 2) (check-expect (count-doubles (list 1 1 2 2)) 2) (check-expect (count-doubles (list 1 1 1 1 5 5 4 4 3)) 5) (check-expect (count-doubles (list 1)) 0) (check-expect (count-doubles empty) 0) ; A ftn is either ; - empty ; - (make-child name date eyes mom dad) ; where a child is ; - symbol name ; - number date (a year) ; - symbol eyes (a color) ; - ftn mom ; - ftn dad (define-struct child (name date eyes mom dad)) ; template: ; (define (fun-for-ftn a-ftree) ; (cond ; [(empty? a-ftree) ...] ; [else ; ... (child-name a-ftree) ... ; ... (child-date a-ftree) ... ; ... (child-eyes a-ftree) ... ; ... (fun-for-ftn (child-dad a-ftree)) ... ; ... (fun-for-ftn (child-mom a-ftree)) ... ;])) ; examples: ;; Grandparents (define Dave (make-child 'Dave 1937 'black empty empty)) (define Eva (make-child 'Eva 1934 'blue empty empty)) (define Fred (make-child 'Fred 1930 'pink empty empty)) (define Greta (make-child 'Greta 1933 'brown empty empty)) ;; Parents (define Carl (make-child 'Carl 1967 'green Eva Dave)) (define Bettina (make-child 'Bettina 1966 'green Greta Fred)) ;; Youngest Generation: (define Adam (make-child 'Adam 1990 'yellow Bettina Carl)) ;; Q3 write the following function: ; oldest-ancestor: ftn -> ftn ; returns the oldest ancestor in the family tree (i.e. the one born first) (define (oldest-ancestor a-ftree) (cond [(empty? a-ftree) empty] [(empty-parents a-ftree) a-ftree] [else (older (oldest-ancestor (child-mom a-ftree)) (oldest-ancestor (child-dad a-ftree)))] )) ; helpers: ; older ftn ftn -> ftn ; returns which of the two ftns is older (define (older ft1 ft2) (cond [(empty? ft1) ft2] [(empty? ft2) ft1] [else (if (< (child-date ft1) (child-date ft2)) ft1 ft2)])) (check-expect (older Dave Fred) Fred) (check-expect (older empty Greta) Greta) ; empty-parents: ftn -> boolean ; returns whether the ftn has non-empty parents (define (empty-parents ft) (and (empty? (child-mom ft)) (empty? (child-dad ft)))) (check-expect (empty-parents Dave) true) (check-expect (empty-parents Adam) false) (check-expect (oldest-ancestor Adam) Fred) (check-expect (oldest-ancestor Greta) Greta) (check-expect (oldest-ancestor empty) empty)