;; recursion that processes a given list ;; list-of-squares: list of numbers -> list of numbers ;; The function takes a list of numbers and produces ;; a list of their squares ;; ;; Example: ;; (list-of-squares (cons 2 (cons -1 (cons 5 empty)))) -> ;; (cons 4 (cons 1 (cons 25 empty))) (define (list-of-squares lon) (cond [(empty? lon) empty] [else (cons (expt (first lon) 2) (list-of-squares (rest lon)))] ) ) (list-of-squares empty) ;; expected answer empty (list-of-squares (cons 2 (cons -1 (cons 5 empty)))) ;; expected answer (cons 4 (cons 1 (cons 25 empty))) ;; recursion that generates lists ;; n-numbers: integer -> list of numbers ;; the function takes an integer n >= 0 and returns a list ;; of the first n numbers in order, starting at 1 ;; ;; Example: ;; (n-numbers 2) ->(cons 1 (cons 2 empty)) (define (n-numbers n) (cond [(<= n 0) empty] [else (cons n (n-numbers (- n 1)))] ) ) (n-numbers 0) ;; expected answer empty (n-numbers 2) ;; expected answer (cons 1 (cons 2 empty)) ;; write a function that takes an integer n>=0 and returns ;; a list of n even numbers 2,4,6,... ;; recursion on numbers (no lists): ;; write a function factorial that takes an integer n >= 1 ;; and computes n! = 1*2*...*n ;; write a function sym-list=? that, given two lists of symbols, ;; returns true if the two lists are exactly the same (same ;; length, same elements), and false otherwise ;; a (pseudo)random number generator ;; takes n, returns a number in the range 0 to n-1 (random 5) (random 5) (random 5) ;; random-n: integer -> list of numbers ;; takes a number n>=0 and produces a list of n pseudo-random ;; numbers in the range 0 to 99 ;; ;; Example: ;; (random-n 3) -> (cons 57 (cons 23 (cons 98 empty))) ;; Note that the actual nubmers will be different every time ;; the function runs (define (random-n n) (cond [(= n 0) empty] [else (cons (random 100) (random-n (- n 1)))] ) ) (random-n 5) (random-n 3) (random-n 0)