;; ::= '() ;; ::= (cons ) ;; got-milk? : -> ;; (got-milk? '()) ->-> #f ;; (got-milk? '(apple milk bread)) ->-> #t ;; (got-milk? '(apple bread)) ->-> #f ;; Template: ;(define (got-milk? l) ; (cond ; [(null? l) ...] ; [(pair? l) ... (car l) ... (got-milk? (cdr l)) ... ])) (define (got-milk? l) (cond [(null? l) #f] [(pair? l) (or (eq? 'milk (car l)) (got-milk? (cdr l))) ])) ;; By putting tests here, they get run when we ;; click Execute: (got-milk? '()) ;; ->-> #f (got-milk? '(apple milk bread)) ;; ->-> #t (got-milk? '(apple bread)) ;; ->-> #f ;; : -> ;; (subst '() 'x 'y) ->-> '() ;; (subst '(w x z) 'x 'y) ->-> '(w y z) ;; (subst '(w x x z) 'x 'y) ->-> '(w y y z) ;(define (subst l a b) ; (cond ; [(null? l) ...] ; [(pair? l) ... (car l) ... (subst (cdr l) a b) ...])) (define (subst l a b) (cond [(null? l) '()] [(pair? l) (cons (cond [(eq? a (car l)) b] [else (car l)]) (subst (cdr l) a b))])) ;; Or (define (subst l a b) (cond [(null? l) '()] [(pair? l) (cond [(eq? a (car l)) (cons b (subst (cdr l) a b))] [else (cons (car l) (subst (cdr l) a b))])])) ;; Or, in professional life but NOT HW 1 (define (subst l a b) (cond [(null? l) '()] [(eq? a (car l)) (cons b (subst (cdr l) a b))] [else (cons (car l) (subst (cdr l) a b))])) ;; For HW 1, please leave the function un-simplified, ;; so that it matches the shape of the data definition. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ::= ;; ::= (cons ) ;; ::= '() ;; Example members of : 'a (cons '() 'a) '() ;; got-milk2? : -> ;; (got-milk2? 'milk) ->-> #t ;; (got-milk2? 'malk) ->-> #f ;; (got-milk2? (cons '() 'a)) ->-> #f ;; (got-milk2? (cons '() 'milk)) ->-> #t ;; (got-milk2? '()) ->-> #f ;(define (got-milk2? l) ; (cond ; [(symbol? l) ... l ...] ; [(pair? l) ... (got-milk2? (car l))...(got-milk2? (cdr l))...] ; [(null? l) ...])) (define (got-milk2? l) (cond [(symbol? l) (eq? 'milk l)] [(pair? l) (or (got-milk2? (car l)) (got-milk2? (cdr l)))] [(null? l) #f])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ::= 0 ;; ::= (+ 1 ) ;; ! : -> ;; (! 0) ->-> 1 ;; (! 5) ->-> 120 ;(define (! n) ; (cond ; [(zero? n) ... ] ; [else ... (! (- n 1)) ... ])) (define (! n) (cond [(zero? n) 1] [else (* n (! (- n 1)))]))