;; 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 |lists of structs|) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; Helene Martin, Garfield high school ;; 10/6/2010 ;; Lists and lists of structs review ;; A list-of-number is ;; - empty, or ;; - (cons number list-of-number) ;; Template over a list-of-number ;;(define (lon-template alon) ;; (cond [(empty? alon) ...] ;; [(cons? alon) ... (first alon) ;; ... (lon-template (rest alon)) ... ])) (define LIST1 (cons 2 (cons 4 (cons 1 (cons 3 empty))))) (define LIST2 (cons 6 (cons 5 (cons 4 (cons 8 (cons 3 empty)))))) ;; sort: list-of-number -> list-of-number (define (sort-it a-lon) (cond [(empty? a-lon) empty] [else (insert (first a-lon) (sort-it (rest a-lon)))])) ;; insert: number list-of-number -> list-of-number ;; inserts an element into a sorted list, preserving sorted order (define (insert num a-lon) (cond [(empty? a-lon) (cons num empty)] [else (cond [(<= num (first a-lon)) (cons num a-lon)] [else (cons (first a-lon) (insert num (rest a-lon)))])])) (check-expect (insert 2 empty) (cons 2 empty)) (check-expect (insert 2 (cons 3 empty)) (cons 2 (cons 3 empty))) (check-expect (insert 2 (cons 1 (cons 3 empty))) (cons 1 (cons 2 (cons 3 empty)))) ;; A student is (make-student string number) ;; Student ids start with the graduation year (11xxx for those who will graduate in '11) (define-struct student (name id)) (define ADAM (make-student "Adam" 11123)) (define MICHAEL (make-student "Michael" 13123)) (define QUYNH (make-student "Quynh" 11234)) (define LANE (make-student "Lane" 13234)) (define STUDENTS (cons ADAM (cons MICHAEL (cons QUYNH (cons LANE empty))))) ;; Template over a student ;;(define (student-template a-student) ;; ... (student-name a-student) ... ;; ... (student-id a-student) ...) ;; A list-of-student ;; - empty, or ;; - (cons student list-of-student) ;; Template over a list of students ;;(define (los-template a-los) ;; (cond [(empty? a-los) ...] ;; [(cons? a-los) ... (student-template (first a-los)) ;; ... (lon-template (rest a-los)) ... ])) ;; senior?: student -> boolean (define (senior? a-student) (and (>= (student-id a-student) 11000) (< 1200 (student-id a-student) 12000))) (check-expect (senior? QUYNH) true) (check-expect (senior? MICHAEL) false) ;; extract-seniors: list-of-student -> list-of-student (define (extract-seniors a-los) (cond [(empty? a-los) empty] [else (cond [(senior? (first a-los)) (cons (first a-los) (extract-seniors (rest a-los)))] [else (extract-seniors (rest a-los))])])) (check-expect (extract-seniors STUDENTS) (cons ADAM (cons QUYNH empty)))