(require 2htdp/image) (require 2htdp/universe) ;========== ; CS 111 - Week 5 Labs - list-related reminders! ;========== ;----- ; data definition: ; an Anything is an expression of ANY type ;******************************* ;========== ; data definition: ; a list is: ; - empty, OR ; - (cons Anything list) ;******************************* ;----- ; data definition: ; a NonEmptyList is: ; - (cons Anything empty), OR ; - (cons Anything NonEmptyList) ;----- ; signature: cons: Anything list -> list ; purpose: expects any expression (that has a value) ; and a list, and returns (CONStructs) a new list with ; that expression as its first value, and ; everything else in the given list ; as the rest of the list ; this is an empty list empty ; this is a list with 1 thing in it: (cons "hi" empty) ; this is a list with 4 things in it: (cons "moo" (cons 6 (cons #true (cons (circle 10 "solid" "blue") empty)))) ;----- ; signature: first: NonEmptyList -> Anything ; purpose: expects a non-empty list, and returns ; its first item (first (cons "moo" (cons 6 (cons #true (cons (circle 10 "solid" "blue") empty))))) ;----- ; signature: rest: NonEmptyList -> list ; purpose: expects a non-empty list, and returns ; a new list of all BUT the first element ; in the given list ; ; remember: rest ALWAYS returns a list!! (rest (cons "moo" (cons 6 (cons #true (cons (circle 10 "solid" "blue") empty))))) (rest (cons 56 empty)) ;----- ; signature: empty?: Anything -> boolean ; purpose: expects any expression (with a value), ; and returns #true if it is an empty list, ; and returns #false otherwise (empty? empty) (empty? (cons "looky" empty)) (empty? "howdy") (empty? 0) ;********************************************* ; list function template - COMPLETE ; ; template for a function that "walks through" ; a list ;============================================== ; (define (a-list-funct a-list ...) ; (cond ; [(empty? a-list) ...] ; [else (... (first a-list) ... ; (a-list-funct (rest a-list) ...) ...)] ; ) ; ) ;********************************** ; DATA DEFINITION ; a NumberList is one of: ; - empty ; - (cons number NumberList) ;**************************** ; NumberList function template ; ; template for a function that "walks through" a NumberList ;============================================== ; (define (num-list-funct a-num-list ...) ; (cond ; [(empty? a-num-list) ...] ; [else (... (first a-num-list) ... ; (num-list-funct (rest a-num-list) ...) ...)] ; ) ; )