;; 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-beginner-reader.ss" "lang")((modname lect23-lambda-map) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; 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 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 containgin 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)))])) (map sqr (list 9 3 21 4)) ; 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 (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; } (big-bang (make-world ...) update-world ; called on-tick draw-world ; called on-tick world-handle-key ; called on every key-event world-is-over? ; called on-tick, to determine i done ) ; world-is-over? : world -> boolean (define (world-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,key -> world (λ (w k) (make-world (world-ball w) (world-birkcs w) (paddle-handle-key (world-paddle w) k))) (λ (w) (> (ball-y (world-ball w)) WORLD-HEIGHT)) ) (define blah 99)