;; 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 lect25-dist-lambda-map) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; A list-of-number is: ; - empty, OR ; - (cons [number] [list-of-number]) ;;; template for *any* function processing a list: ; func-for-list : list-of-number -> ??? ; (define (func-for-list threshold nums) (cond [(empty? nums) 0] [(cons? nums) (... (first nums) ... (func-for-list (rest nums)))])) ; Examples of data: empty (cons 5 empty) (cons 7 (cons 5 empty)) (cons 2 (cons 7 (cons 5 empty))) ; count-bigs : number, list-of-number → natnum ; return how many items in `nums` are > `threshold`. ; #;(define (count-bigs threshold nums) (cond [(empty? nums) 0] [(cons? nums) (if (> (first nums) threshold) (+ 1 (count-bigs threshold (rest nums))) (count-bigs threshold (rest nums)))])) (define (count-bigs threshold nums) (cond [(empty? nums) 0] [(cons? nums) (+ (if (> (first nums) threshold) 1 0) (count-bigs threshold (rest nums)))])) (check-expect (count-bigs 7 empty) 0) (check-expect (count-bigs 7 (cons 5 empty)) 0) (check-expect (count-bigs 7 (cons 7 empty)) 0) (check-expect (count-bigs 7 (cons 9 empty)) 1) (check-expect (count-bigs 7 (cons 3 (cons 7 empty))) 0) (check-expect (count-bigs 7 (cons 9 (cons 7 empty))) 1) (check-expect (count-bigs 7 (cons 3 (cons 9 empty))) 1) (check-expect (count-bigs 7 (cons 8 (cons 9 empty))) 2) (check-expect (count-bigs 7 (cons 3 (cons 7 (cons 8 (cons 2 empty))))) 1) ; map-sqr : list-of-number → list-of-number ; return a list containing the square of each number in `nums` ; (define (map-sqr nums) (cond [(empty? nums) empty] [(cons? nums) (cons (sqr (first nums)) (map-sqr (rest nums)))])) (define (my-map hammer nums) (cond [(empty? nums) empty] [(cons? nums) (cons (hammer (first nums)) (map hammer (rest nums)))])) ;; Imagine: if our breakout had several balls simultaneously ;; -- supposing the world had a field `balls` that was a list-of-balls. ;(map move-ball (world-balls w)) ;(map update-paddle (world-paddles w)) ;; Functions are values! ;; "functions are first-class values, just like numbers and booleans..." (map sqr (list 9 3 21 4)) (define my-favorite-names (list "Fred" "Amy" "George" "Betty")) ; `list` is a function that takes its args, and returns ; the cons ... cons ... cons. ... empty. ; HINT: Never use `list` inside your functions -- you will ; want to be using *cons*, to tack one item onto an existing list. ; HOWEVER, `list` is particularly handy when writing test-cases ; and examples-of-data. (map string-length my-favorite-names) ; For now: ; use `list` ONLY when making test-inputs -- never inside a function. (check-expect (map-sqr empty) empty) (check-expect (map-sqr (cons 7 empty)) (cons 49 empty)) (check-expect (map-sqr (cons 9 (cons 7 empty))) (cons 81 (cons 49 empty))) ; Functions are "first class" values. ; (this is one aspect of "functional programming") (define pie 22/7) #;(define (triplify n) (* 3 n)) ; is ACTUALLY shorthand for: (define triplify (lambda (n) (* 3 n))) ;(λ (n) (* 3 n)) #| (triplify (+ 2 blah)) => (triplify 101) => ((λ (n) (* 3 n)) 101) => (* 3 101) => 303 ; In php: ; $triplify = function (n) { return 3*n; } ; In Java: ; ..... ; In Java 8: (n) -> { return 3*n; } ; world -> boolean ; Is the game over? ; (define (game-is-over? w) (> (ball-y (world-ball w)) WORLD_HEIGHT)) (big-bang (make-world ...) update-world ; called on-tick draw-world ; called on-tick world-handle-key ; called on every key-event game-is-over? ; called on-tick, to determine i done ) (big-bang (make-world ...) [on-tick update-world] ; called on-tick [on-draw draw-world] ; called on-tick ; world,key -> world ; ; (λ (w k) (make-world (world-ball w) (world-bricks w) (paddle-handle-key (world-paddle w) k))) ; world -> boolean ; we want a function that just tells when the game is over: ; when the ball goes off bottom of screen. [stop-when (lambda (w) (> (ball-y (world-ball w)) WORLD-HEIGHT))] ; rather than: [stop-when game-is-over?] ) |#