;; ormap : [X -> Boolean] [Listof X] -> Boolean ;; Does any element of lox satisfy the test? (define (my-ormap test lox) (cond [(empty? lox) false] [else (or (test (first lox)) (my-ormap test (rest lox)))])) ;; self-collide? : Posn [Listof Posn] -> Boolean ;; does the head overlap any of the body segments(posns)? (define (self-collide? head body) (local (;;Posn Posn -> Boolean (define (head=? p1) (and (= (posn-x p1) (posn-x head)) (= (posn-y p1) (posn-y head))))) (ormap head=? body))) (check-expect (self-collide? (make-posn 1 3) empty) false) (check-expect (self-collide? (make-posn 1 3) (list (make-posn 2 3))) false) (check-expect (self-collide? (make-posn 1 3) (list (make-posn 2 3) (make-posn 1 3))) true) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; andmap : [X -> Boolean] [Listof X] -> Boolean ;; Do all elements of lox satisfy the test? (define (my-andmap test lox) (cond [(empty? lox) true] [else (and (test (first lox)) (my-andmap test (rest lox)))])) ;; all-gt10? : [Listof Number] -> Boolean ;; Is every number in lon greater than 10? ;; initial version using local (define (all-gt10?.v1 lon) (local (;; gt10? : Number -> Boolean (define (gt10? n) (> n 10))) (andmap gt10? lon))) ;; using lambda (define (all-gt10? lon) (andmap (lambda (n) (> n 10)) lon)) (check-expect (all-gt10? '()) true) (check-expect (all-gt10? '(40 20 10 3 2)) false) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; my-length : [Listof X] -> Number ;; compute length of given list ;; initial version, written recursively (define (my-length.v0 lox) (cond [(empty? lox) 0] [else (add1 (my-length (rest lox)))])) ;; version using foldr and local (define (my-length.v1 lox) (local (;; X Number -> Number (define (add1-len x n) (add1 n))) (foldr add1-len 0 lox))) ;; using foldr and lambda (define (my-length lox) (foldr (lambda (x n) (add1 n)) 0 lox)) (check-expect (my-length empty) 0) (check-expect (my-length '(10 30)) 2) (check-expect (all-gt10? '(40 20)) true) #| Note that the following are equivalent: (define (gt10? n) (> n 10)) (define gt10? (lambda (n) (> n 10))) |#