#lang racket ;; CSE 413 14au ;; Lecture 7 sample code ;; Hal Perkins ;; return true if x and y have the same shape (define same-shape (lambda (x y) (cond ((and (pair? x) (pair? y)) (and (same-shape (car x) (car y)) (same-shape (cdr x) (cdr y)))) ((or (pair? x) (pair? y)) #f) (else #t)))) ;; (same-shape '(a b c) '(x y z)) ;; (same-shape '(a b c) '(a (b c))) ;; (same-shape '(a (b) c) '(w (x) y)) ;; given a list of numbers, return a sorted list of those numebers ;; (insertion sort) (define isort (lambda (lst) (if (null? lst) '() (insert (car lst) (isort (cdr lst)))))) ;; return copy of lst with x inserted in proper place so result is sorted (define insert (lambda (x lst) (cond ((null? lst) (list x)) ((< x (car lst)) (cons x lst)) (else (cons (car lst) (insert x (cdr lst))))))) ;; return "n is even" for n >= 0 (define is-it-even? (lambda (n) (letrec ((is-even? (lambda (n) (if (= n 0) #t (is-odd? (- n 1))))) (is-odd? (lambda (n) (if (= n 0) #f (is-even? (- n 1)))))) (is-even? n))))