;; 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 lect04c) (read-case-sensitive #t) (teachpacks ((lib "universe.ss" "teachpack" "2htdp") (lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.ss" "teachpack" "2htdp") (lib "image.ss" "teachpack" "2htdp"))))) ; - let for root1 #| (define a 17) (define discrim "I am *extremely* discriminating") (define (root1 a b c) (let* {[discrim (- (* b b) (* 4 a c))] [numer (+ (- b) (sqrt discrim))]} (/ numer (* 2 a)))) (check-expect (root1 1 0 -1) 1) ;(check-expect (root1 0 3 -3) 1) (check-expect (root1 7 23 (- 23 7)) -1) |# ; my-min : list-of-numbers -> number ; (define (my-min data) (cond [(empty? data) +inf.0] [(cons? data) (if (< (first data) (my-min (rest data))) (first data) (my-min (rest data)))])) (check-expect (my-min (cons 4 (cons 7 empty))) 4) (check-expect (my-min (cons 7 empty)) 7) (check-expect (= (my-min empty) +inf.0) true) (check-expect (my-min (cons 7 (cons 2 (cons 7 empty)))) 2) (check-expect (my-min (cons -2 empty)) -2) (check-expect (my-min (cons -8 (cons -7 (cons -6 (cons -5 empty))))) -8) ;;; in truth: max should only be defined for *non*-empty lists. ;;; (max empty) is inherently undefined. ;;; (The '-inf.0' trick is okay, but you can't use the same trick ;;; for (say) min-string. ;;; ;;; One approach would involve using a modified data def'n: ;;; ;; A non-empty list of numbers is either: ;; (cons num empty) ;; OR ;; (cons num nelon) ;; ; num : number ;; ; nelon : non-empty list of number ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Some new data definition ; ;; A natnum is: ;; 0 ;; OR ;; (+ 1 n) ;; ; n : natnum ;; examples of natnums: ;; 0 1 2 3 4 .... ;; 0, S(0), S(S(0)), S(S(S(0))), .... ;; template: ;; func-for-natnum : natnum -> ??? #;(define (func-for-natnum k) (cond [(zero? k) ...] [(positive? k) ...k... (func-for-natnum (- k 1))...])) (define (countdown k) (cond [(zero? k) "liftoff!"] [(positive? k) (string-append (number->string k) ".." (countdown (sub1 k)))])) (check-expect (countdown 3) "3..2..1..liftoff!") (check-expect (countdown 1) "1..liftoff!") (check-expect (countdown 0) "liftoff!") ; nums-down-from : natnum -> (list-of natnum) (check-expect (nums-down-from 0) empty) (check-expect (nums-down-from 1) (cons 1 empty)) (check-expect (nums-down-from 2) (cons 2 (cons 1 empty))) (check-expect (nums-down-from 3) (cons 3 (cons 2 (cons 1 empty)))) (define (nums-down-from k) (cond [(zero? k) empty] [(positive? k) (cons k (nums-down-from (- k 1)))])) (time (my-min (nums-down-from 10))) (time (my-min (nums-down-from 20))) (time (my-min (nums-down-from 21))) (define (my-min-v2 data) (cond [(empty? data) +inf.0] [(cons? data) (let {[mm (my-min-v2 (rest data))]} (if (< (first data) mm) (first data) mm))])) (check-expect (my-min-v2 (cons 4 (cons 7 empty))) 4) (check-expect (my-min-v2 (cons 7 empty)) 7) (check-expect (= (my-min-v2 empty) +inf.0) true) (check-expect (my-min-v2 (cons 7 (cons 2 (cons 7 empty)))) 2) (check-expect (my-min-v2 (cons -2 empty)) -2) (check-expect (my-min-v2 (cons -8 (cons -7 (cons -6 (cons -5 empty))))) -8) #| cond -- if-else-if cons -- the constructor for non-empty-lists... "for cons objects" (cons 3 empty) -- I'll say it *is* hte list, but it could also be "the constructor-call". Racket chooses to print out data *the same way* you would call its constructor. cons? -- a predicate any -> boolean |# ;; A filenode is ;; - a file, ;; OR ;; - a directory ;; OR ;; - a symlink ;; (directory-list) ;; (directory-list "/Users/ibarland") ; data def'n for natnums ; - countdown ; - nums-down ; - time my-min ; - let ; accum