;; Homework #3 ;; D.S. Blank ;; With length: (define shorter1 (lambda (l1 l2) (if (< (length l1) (length l2)) l1 l2))) ;; Pass in copy: (define shorter2 (lambda (l1 l2 c1 c2) (cond ((null? l1) c1) ((null? l2) c2) (else (shorter2 (cdr l1) (cdr l2) c1 c2))))) ;; Return two things: (define shorter3 (lambda (l1 l2) (cond ((null? l1) '(#t ())) ((null? l2) '(#f ())) ((car (shorter3 (cdr l1) (cdr l2))) (list #t l1)) (else (list #f l2))))) ;; Pass in two empty lists, build up as you go, then reverse (define shorter4 (lambda (l1 l2 stuff1 stuff2) (cond ((null? l1) (reverse stuff1)) ((null? l2) (reverse stuff2)) (else (shorter4 (cdr l1) (cdr l2) (cons (car l1) stuff1) (cons (car l2) stuff2)))))) ;; Is first one shorter? Needs helper function (define shorter5-helper (lambda (l1 l2) (cond ((null? l1) #t) ((null? l2) #f) (else (shorter5-helper (cdr l1) (cdr l2)))))) (define shorter5 (lambda (l1 l2) (if (shorter5-helper l1 l2) l1 l2))) (define odd? (lambda (n) (cond ((= n 0) #f) (else (even? (- n 1)))))) (define even? (lambda (n) (cond ((= n 0) #t) (else (odd? (- n 1)))))) (define make-counter (lambda (init incr) (let ((next init)) (lambda () (let ((v next)) (set! next (+ next incr)) v))))) (define by2 (make-counter 0 2)) (define by3 (make-counter 0 3)) (define 100by10 (make-counter 100 10))