;; 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-intermediate-lambda-reader.ss" "lang")((modname week7_w._fullrkt) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; Structural recursion ; right from template ; list-of-numbers is either: ; - empty, or ; - (cons number list-of-numbers) ; max-list : list-of-numbers -> number ; returns the largest element in a list of (positive) numbers (define (max-list lon) (cond [(empty? lon) 0] [else (max (first lon) (max-list (rest lon)))] )) (check-expect (max-list (list 1 2 3)) 3) ; Generative Recursion ; you have to invent the recursive call (using your brilliance) ; factorial : number -> number ; computes n! (define (factorial n) (cond [(= n 1) 1] [else (* n (factorial (- n 1)))]) ) (check-expect (factorial 5) 120) ; fib : n -> fib(n) ; computes nth fibonacci number where f_n = f_(n-1) + f_n-2) ; and f_0 = f_1 = 1 (define (fib n) (cond [(= n 0) 1] [(= n 1) 1] [else (+ (fib (- n 1)) (fib (- n 2)))] )) (check-expect (fib 5) 8)