; Streams Example (define s (cons-stream 1 (cons-stream 2 (/ 1 0)) ) ) ; Recursive implementation of factorial (define (fact-r n) (if (= n 0) 1 (* n (fact-r (- n 1))) ) ) ; Tail recursive implementation of k * n! (define (fact-alt n k) (if (= n 0) k (fact-alt (- n 1) (* k n)) ) ) ; Tail recursive implementation of factorial (define (fact-i n) (define (helper total n) (if (= n 0) total (helper (* total n) (- n 1)) ) ) (helper 1 n) ) ; Non tail recursive implementation of length (define (length lst) (+ 1 (if (null? lst) -1 (length (cdr lst)) ) ) ) ; Tail recursive implementation of contains (define (contains s v) (if (null? s) #f (if (= v (car s)) #t (contains (cdr s) v) ) ) ) ; Non tail recursive implementation of length (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst))) ) ) ; Tail recursive implementation of length (define (length lst) (define (helper lst total) (if (null? lst) total (helper (cdr lst) (+ 1 total)) ) ) (helper lst 0) ) ; Tail recursive implementation of reverse (define (reverse lst) (define (helper lst r) (if (null? lst) r (helper (cdr lst) (cons (car lst) r)) ) ) (helper lst '()) ) ; Tail recursive implementation of map (define (map fn lst) (define (helper lst res) (if (null? lst) res (helper (cdr lst) (cons (fn (car lst)) res)) ) ) (reverse (helper lst '())) )