;; A lon is a list of numbers (define myList1 (list 5 4 3 2 1)) (define myList2 (list -2 -1 0 1 2 )) ;; fine_add: lon -> num ;; adds up all the numbers in a lon (define (fine_add a-lon) (cond [(empty? a-lon) 0] [(cons? a-lon) (+ (first a-lon) (fine_add (rest a-lon)))])) "fine_add tests:" (= 0 (fine_add empty)) (= 15 (fine_add myList1)) (= 0 (fine_add myList2)) ;; bad_add: lon -> num ;; adds up all the numbers in a lon (define (bad_add a-lon) (cond [(empty? a-lon) 0] [(cons? a-lon) (cond [(empty? (rest a-lon)) (first a-lon)] [(cons? (rest a-lon)) (+ (first a-lon) (bad_add (rest a-lon)))])])) "bad_add tests:" (= 0 (bad_add empty)) (= 15 (bad_add myList1)) (= 0 (bad_add myList2)) ;; good_add: lon -> num ;; adds up all the numbers in a lon (define (good_add a-lon) (cond [(empty? a-lon) 0] [(cons? a-lon) (good_add_help (rest a-lon) (first a-lon))])) ;; good_add_help: a-lon num -> num ;; adds all the numbers in a lon to the supplied accumulator ;; and returns the result. (define (good_add_help a-lon acc) (cond [(empty? a-lon) acc] [(cons? a-lon) (good_add_help (rest a-lon) (+ (first a-lon) acc))])) "good_add tests:" (= 0 (good_add empty)) (= 15 (good_add myList1)) (= 0 (good_add myList2)) ;; good_add2: lon -> num ;; adds up all the numbers in a lon (define (good_add2 a-lon) (good_add_help a-lon 0)) "good_add2 tests:" (= 0 (good_add2 empty)) (= 15 (good_add2 myList1)) (= 0 (good_add2 myList2)) ;; good_add3: lon -> num ;; adds up all the numbers in a lon (define (good_add3 a-lon) (cond [(empty? a-lon) 0] [(cons? a-lon) (good_add_help a-lon 0)])) "good_add3 tests:" (= 0 (good_add3 empty)) (= 15 (good_add3 myList1)) (= 0 (good_add3 myList2)) ;; a NElon is a non-empty list of numbers ;; note that a NElon is a lon. ;; bad_last: NElon -> num ;; returns the last element of a NElon #| Template: (define (f-NELon a-nelon...) (cond [(cons? a-nelon)( ... (first a-nelon)...(rest a-nelon)...)])) |# ;; note that the empty? clause is missing in the template. (define (bad_last a-nelon) (cond [(cons? a-nelon) (cond [(empty? (rest a-nelon)) (first a-nelon)] [(cons? (rest a-nelon)) (bad_last (rest a-nelon))])])) "bad_last tests:" (= 1 (bad_last myList1)) (= 2 (bad_last myList2)) ;; good_last: NElon -> num or sym ;; returns the last element of a NElon (define (good_last a-nelon) (cond [(cons? a-nelon)( good_last_help (rest a-nelon) (first a-nelon))])) ;; good_last_help: lon, num -> num ;; returns the last element of a lon ;; or the accumulator if the lon is empty. (define (good_last_help a-lon acc) (cond [(empty? a-lon) acc] [(cons? a-lon)( good_last_help (rest a-lon) (first a-lon))])) "good_last tests:" (= 1 (good_last myList1)) (= 2 (good_last myList2)) ;; reverse_list: list -> list ;; reverses the elements in a list, returning a new list. (define (reverse_list l) (cond [(empty? l) empty] [(cons? l) (reverse_help (rest l) (cons (first l) empty))])) ;; reverse_help: list list --> list ;; if the supplied list, l, is empty, returns the accumulated list, acc_l. ;; if the supplied list is cons, returns l reversed. (define (reverse_help l acc_l) (cond [(empty? l) acc_l] [(cons? l) (reverse_help (rest l) (cons (first l) acc_l))])) "reverse_list test cases:" (equal? (list 5 4 3 2 1) (reverse_list (list 1 2 3 4 5))) (equal? (list 'a 'b 'c 'd 'e) (reverse_list (list 'e 'd 'c 'b 'a))) (equal? empty (reverse_list empty)) ;; reverse_list2: list -> list ;; reverses the elements in a list, returning a new list. (define (reverse_list2 l) (reverse_help l empty)) "reverse_list2 test cases:" (equal? (list 5 4 3 2 1) (reverse_list2 (list 1 2 3 4 5))) (equal? (list 'a 'b 'c 'd 'e) (reverse_list2 (list 'e 'd 'c 'b 'a))) (equal? empty (reverse_list2 empty)) ;; reverse_list3: list -> list ;; reverses the elements in a list, returning a new list. (define (reverse_list3 l) (cond [(empty? l) empty] [(cons? l) (reverse_help l empty)])) "reverse_list3 test cases:" (equal? (list 5 4 3 2 1) (reverse_list3 (list 1 2 3 4 5))) (equal? (list 'a 'b 'c 'd 'e) (reverse_list3 (list 'e 'd 'c 'b 'a))) (equal? empty (reverse_list3 empty)) ;;pretty-print: list-of-sym --> string ;; returns a string representation of the given list, los, ;;as a string with parantheses around the elements ;; and spaces between the elements ;; but no space between the first/last element and the beginning/closing paranthesis. ;; The empty list is returned as "()" (define (pretty-print los) (cond [(empty? los) "()"] [(cons? los) (string-append "(" (symbol->string (first los)) (pretty-printhelp (rest los)) ")")])) ;; pretty-printhelp: list-of-sym --> string ;; returns a string representation of the given list-of-sym, los, ;; as a string where each element is preceded by a space. ;;The empty list is returned as an empty string. (define (pretty-printhelp los) (cond [(empty? los) ""] [(cons? los) (string-append " " (symbol->string (first los)) (pretty-printhelp (rest los)))])) "Pretty-print test cases:" (equal? "()" (pretty-print empty)) (equal? "(a)" (pretty-print (cons 'a empty))) (equal? "(a b)" (pretty-print (cons 'a (cons 'b empty)))) (equal? "(a b c)" (pretty-print (cons 'a (cons 'b (cons 'c empty)))))