List is the fundamental element in Scheme1).
;;contains? : number list-of-number -> Boolran
;;to determine whether elmt is contained in the lst
;;example (contains? 3 (cons 1 (cons 3 empty))) => true
(define (contains? elmt lst)
(cond
[(empty? lst)
false]
[else
(cond
[(= elmt (first lst)) ;if first of lst is elmt, return true
true]
[else ;else, do the same with (rest lst) until lst is empty
(contains? elmt (rest lst))])]))
code
;;substitute : number number list-of-number -> list-of-number
;;to substitute target in the lst with repl, and produce the
;;replaced list
;;example : (substitute 1 2 (cons 3 (cons 1 empty))) => (cons 3 (cons 2 empty))
(define (substitute target repl lst)
(cond
[(empty? (rest lst)) ;if lst contains only one element
(cond
[(= (first lst) target)
(cons repl empty)]
[else
lst])]
[else ;else, build the first element with rest list
(cond
[(= (first lst) target)
(cons
repl
(substitute target repl (rest lst)))]
[else
(cons
(first lst)
(substitute target repl (rest lst)))])]))
Remove single or duplicated code
;;remove-this : number list-of-number -> list-of-number
;;to remove elmt from lst, procude the processed list
;;example : (remove-this 0 (list 0 1 0)) => (list 1)
(define (remove-this elmt lst)
(cond
[(empty? (rest lst)) ;if lst contains only one element
(cond
[(= (first lst) elmt)
empty]
[else
lst])]
[else
(cond
[(not (= (first lst) elmt))
(cons
(first lst)
(remove-this elmt (rest lst)))]
[else
(remove-this elmt (rest lst))])]))
;;remove-duplicates : list-of-number -> list-of-number
;;to remove all the first occurrence numbers
;;example (remove-duplicates (list 1 4 2 1 5 4)) => (list 1 4 2 5)
(define (remove-duplicates lst)
(cond
[(empty? lst) lst]
[else
(cons
(first lst) ;pick up the first occurrence number
(remove-duplicates
(remove-this ;remove all numbers which =(first lst)
(first lst)
lst)))]))
;;list-digits : number -> list-of-numbers
;;to list a natural number's digits with one's digits at first of list
;;example : (list-digits 9) => (list 9)
;; (list-digits 10) => (list 0 1)
(define (list-digits n)
(cond
[(< n 10) (cons n empty)]
[else
(cons
(remainder n 10)
(list-digits (quotient n 10)))]))
code
;;mt-tow : number number number -> list-of-number
;;to produce the n'th row; where i is a counter and it should be set to 0 at initial
;;example : (mt-row 2 4 0) => (list 2 4 6 8)
(define (mt-row n m i)
(cond
[(= i m) empty]
[else
(cons
(* n (add1 i))
(mt-row n m (add1 i)))]))
;;mult-table-core : number number number -> list-of-list
;;to produce the mult-table when neither n is zero nor m is zero;
;;where i is a counter and it should be set to 0 at initial
;;example : (mult-table-core 2 2 0) => (list (list 1 2) (list 2 4))
(define (mult-table-core n m i)
(cond
[(= i n) empty]
[else
(cons
(mt-row (add1 i) m 0)
(mult-table-core n m (add1 i)))]))
;;mult-table : number number -> list-of-list
;;to procude a m*n matrix with n rows and m columns
;;example : (mult-table 2 3) => (list (list 1 2 3) (list 2 4 6) (list 3 6 9))
(define (mult-table n m)
(cond
[(or (= n 0) (= m 0)) empty]
[else
(mult-table-core n m 0)]))
code
(define days-in-month (list 31 28 31 30 31 30 31 31 30 31 30 31))
;;is-leap-year : number -> number
;;to determine whether the year is leap; if leap, then return 1; else return 0
(define (is-leap-year year)
(cond
[(= (remainder year 400) 0) ;when year is divisible by 400 the year is leap
1]
[(and ;when year is divisible by 4 but not the century year (-00) the year is leap
(= (remainder year 4) 0)
(not
(= (remainder year 100) 0)))
1]
[else ;else year is not leap year
0]))
;;get-sum-of-days : list-of-number number -> number
;;to compute the sum of days from Jan 1 to the last day of month m
;;example : (get-sum-of-days days-in-month 1) => 31
(define (get-sum-of-days lst m)
(cond
[(= m 0) 0]
[else
(+
(first lst)
(get-sum-of-days (rest lst) (sub1 m)))]))
;;day-within-year : number number number -> number
;;to compute how many days passed from Jan 1 to the day
;;example : (day-within-year 3 1 2000) => 61
(define (day-within-year month day year)
(cond
[(<= month 2)
(+ (get-sum-of-days days-in-month (sub1 month))
day)]
[else
(+
(+ (get-sum-of-days days-in-month (sub1 month))
day)
(is-leap-year year))]))