;; The first three lines of this file were inserted by DrScheme. 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 lect04b) (read-case-sensitive #t) (teachpacks ((lib "world.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "world.ss" "teachpack" "htdp"))))) (filter even? (list 7 8 5 4 2 1 99 100)) (define (<5? n) (< n 5)) (filter <5? (list 7 8 5 4 2 1 99 100)) (define (shorter-than-elhoo? s1) (< (string-length s1) (string-length "elhho"))) (filter shorter-than-elhoo? (list "supercalifragi" "xyz" "hello there" "ello" "hello")) ;;;; NOW, re-write filter-longs by calling the built-in filter. ;;; How? Can't say: ;(define (filter-longs ref alos) ; (filter longer-than-ref? alos)) ; ;(define (longer-than-ref? s1) ; (> (string-length s1) (string-length ref))) ;; Error: 'ref' is only defined inside of filter-longs, not longer-than-ref. (define (filter-longs ref alos) (filter (lambda (s1) (> (string-length s1) (string-length ref))) alos)) ; lambda: creates an anonymous function at *run-time*! ; Now, our test cases (same as last time:) (define words (list "supercalifragi" "xyz" "ehllo there" "aloha" "ehllo")) (check-expect (filter-longs "a very long string" empty) empty) (check-expect (filter-longs "a very long string" (cons "ehllo" empty)) empty) (check-expect (filter-longs "shrty" (cons "ehllo there" empty)) (cons "ehllo there" empty)) (check-expect (filter-longs "shrty" words) (cons "supercalifragi" (cons "ehllo there" empty))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 'define' associates a name with a value: (define my-constant 27) ; But 'define' followed by open-paren defines ; a function instead: ; (define (my-function n) (+ n 17)) ; But really these two are the same thing, ; using lambda: ; The second form of 'define' gets syntactic-sugared into: ; (define my-function-v2 (lambda (n) (+ n 17))) (my-function-v2 8) ; => ((lambda (n) (+ n 17)) 8) ; => (+ 8 17) ; => 25 ; Note that 'my-function' is just a name whose value ; is a function-value (!), in the same way that ; my-constant is just a name whose value is a number. (define + *) (check-expect (+ 3 4 5) 60)