;; 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 lect-24) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; Functions for natNums ; (Sum1..n; nums-down-from; countdown) ; (check-expect (sum-to 0) 0) (check-expect (sum-to 1) 1) (check-expect (sum-to 2) 3) (check-expect (sum-to 3) 6) (check-expect (sum-to 4) 10) (define (func-for-natnum n) (cond [(zero? n) ...] [(postive? n) (... 1 ... (func-for-natnum (sub1 n))) ])) ; sum-to : natnum -> natnum ; Return 0+1+2+...+n (define (sum-to n) 23) (check-expect (nums-down-from 0) empty) (check-expect (nums-down-from 1) (list 1)) (check-expect (nums-down-from 2) (list 2 1)) (check-expect (nums-down-from 3) (list 3 2 1)) ; nums-down-from: natnum -> list-of-natnum ; Return a list of numbers from `k` down to (but not including) 0. ; (define (nums-down-from k) (list 43 -99 2)) (check-expect (nums-up-til 0) empty) (check-expect (nums-up-til 1) (list 0)) (check-expect (nums-up-til 2) (list 0 1)) (check-expect (nums-up-til 3) (list 0 1 2)) ; nums-up-til : natnum -> list-of-natnum ; Return a list of numbers from 0 up to (but not including) k. ; (define (nums-down-from k) (list 43 -99 2)) ;;; What does the template lead us to? ;;; Self-test: complete that approach. ;;; Let's use `map` to write this, instead! ; Racketeers often don't use explicit loops, ; because they're using `map` and `filter`. ; And: `fold`: ;;;;;;;;;;;;;;;;;;;;;;;;; fold ;;;;;;;;;;;;;;;;;;;;;;; ;;;;;; ; A list-of-α is: ; - empty ; OR ; - (cons <α> ) (define (func-for-list data) (cond [(empty? data) ...] [(cons? data) (...(first data) ...(func-for-list (rest data)))])) (check-expect (my-max (cons 4 (cons 7 empty))) 7) (check-expect (my-max (cons 4 (cons 7 empty))) 7) (define (my-max a-lon) (cond [(empty? a-lon) -inf.0] [(cons? a-lon) (if (< (first a-lon) (my-max (rest a-lon))) (my-max (rest a-lon)) (first a-lon))])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Okay, but drawback: stack size grows. ; Here's a different version of draw. ; If you have *tail recursion*, it wouldn't grow. ; ; Assignment: use 'filter', *with a closure*. ; ; finishing higher-order-functions: ; filter; fold. ; Tail recursion ; length