;; 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 lect28) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;;;;;;;;;; ;; Loops in scheme: ; There isn't a 'while' loop built in. ; Can we write our own, as a function? ; How would we call it? (check-expect (nums-down-from 7) (list 7 6 5 4 3 2 1)) ;(sqrts-down-from 17) = (list 4.101 4 3.98 ... 1.414 1) (define (nums-down-from n) (while/list n positive? sub1)) (check-expect (while/list (list "a" "b" "c") cons? rest) (list (list "a" "b" "c") (list "b" "c") (list "c"))) ; while/list: (α -> boolean), α, (α -> α) -> (list-of α) ; Return a list containing `indx`, `(update indx)`, `(update (update indx))`, ... ; so long as `continue?` of that quantity returns true. ; (define (while/list indx continue? update) (reverse (wl-help indx continue? update empty))) ; wl-help : ; Return a list containing `indx`, `(update indx)`, `(update (update indx))`, ... ; so long as `continue?` of that quantity returns true -- ; all prepended to the front of `results-so-far`. ; (define (wl-help indx continue? update result-so-far) (cond [(continue? indx) (wl-help (update indx) continue? update (cons indx result-so-far))] [else result-so-far])) ; Q1: Call while/list to count from 10 to (but not including) 50, by 7s. (while/list 10 (λ (k) (< k 50)) (λ (n) (+ n 7))) ; ; Q2: Write the function 'nums-from-to', ; whose body is just a single call to `while/list`. ; ; Q3: Write `for/list`, which takes a start, stop, and body ; and returns a list of all the body-results. ; i. Implement this w/o calling any of the functions above ; ii. What's the cheapo way of writing this, using functions above? ;;;; (require 2htdp/image) #;(while/list empty-image (λ (img) (< (image-width img) 150)) (λ (img) (let* {[r (image-width img)]} (overlay img (square (+ r 9) 'solid (if (even? r) 'blue 'red)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Review: ; map, filter. ; Given a list of numbers, add 3 to each. (Use map.) ; Given a list of strings, keep only the short ones. ; signature for map ; signature for filter (define fake-pokemon (list "pickatyou" "abracadabrix" "charzander" "pollywog")) (check-expect (my-filter (λ (cp) (<= (string-length cp) 8)) fake-pokemon) (list "pollywog")) (check-expect (my-filter even? (list 2 3 4 5 6 7)) (list 2 4 6)) ; my-filter : (α -> boolean), (listof α) -> (listof α) ; (define (my-filter keep? data) (cond [(empty? data) empty] [(cons? data) (if (keep? (first data)) (cons (first data) (my-filter keep? (rest data))) (my-filter keep? (rest data)))])) #| How to do the same thing in Java (pre-8)? interface Dummy { boolean makeDecision( T cp); } } List myFilter( Dummy keepP, List data ) { if (data.isEmpty()) { return new LinkedList(); } else { List resultOfRecur = myFilter( keepP, data.sublist(1,data.size()) ); if (keepP.makeDecision( data.get(0) ) { return (resultOfRecur).clone().add(0,data.get(0)); else { return resultOfRecur; } } class CounterFeitPokemonKeeper extends Dummy { boolean makeDecision( String cp) { return cp.length() <= 8; } } .... myFilter( new CounterFeitPokemonKeepr(), fakePokemon) .... OR, use anonymuous (inner) classes: .... myFilter( new CounterFeitPokemonKeeper { boolean makeDecision( String cp) { return cp.length() <= 8; }() }, fakePokemon ) ;;;; Java 8: .... myFilter( (cp) -> (cp.length() <= 8), fakePokemon ) ;;; See also: passing a Comparator (lect01); example of passing a LongBinaryOperator. |# #| compare: number of ways to call functions in Java, vs racket: Class.f(...) obj.f(...) (f ...) 2+3 a.x a[4] new Foo(...) casting assign to variable assign to field if? Naw: `? :` methods vs fields: false dichotomy? See: http://bmanolov.free.fr/javaoperators.php |#