;; 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 lect23-dist) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; A natNum is: ; - 0, OR ; - (add1 [natNum]) ; Examples of the data: 0 (add1 0) ; 1 (add1 (add1 0)) ; 2 (add1 (add1 (add1 0))) ; 3 ; Arabic numerals are an awesome algorithm ; for quick adding and writing of natNums. ; Template for natNums: ; func-for-natNum : natNum -> ??? ; (define (func-for-natNum n) (cond [(zero? n) ...] [(positive? n) (...(func-for-natNum (sub1 n))...)])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; lol-times : natNum -> string ; Return `n` copies of "lol" appended. ; (define (lol-times n) (cond [(zero? n) ""] [(positive? n) (string-append "lol" (lol-times (sub1 n)))])) (check-expect (lol-times 0) "") (check-expect (lol-times 1) "lol") (check-expect (lol-times 2) "lollol") (check-expect (lol-times 3) "lollollol") ; ! : natNum -> number ; Return `n * (n-1) * (n-2) * ... * 3 * 2 * 1`. ; (define (! n) (cond [(zero? n) 1] [(positive? n) (* n (! (sub1 n)))])) (check-expect (! 0) 1) (check-expect (! 1) 1) (check-expect (! 2) 2) (check-expect (! 3) 6) (check-expect (! 4) 24) (check-expect (! 5) (* 5 24)) ; nums-down-from : natNum -> list-of-number ; Return a list of numbers from `n` down to 0. ; (define (nums-down-from n) (cond [(zero? n) (cons 0 empty)] [(positive? n) (cons n (nums-down-from (sub1 n)))])) (check-expect (nums-down-from 0) (cons 0 empty)) (check-expect (nums-down-from 1) (cons 1 (cons 0 empty))) (check-expect (nums-down-from 2) (cons 2 (cons 1 (cons 0 empty)))) (check-expect (nums-down-from 3) (cons 3 (cons 2 (cons 1 (cons 0 empty))))) ; recall: ; `cons` can be viewed as a constructor for a struct with 2 fields; ; `cons` is a type (the resulting struct instance); ; `cons` is a function that takes in a list and a single item, ; and tacks that item onto the front of the list. ;; Self-challenge question: ;; Write `image-column` that takes in a natNum n, and an image `img`, ;; and returns an image: `n` stacked copies of `img`. ;; Be sure to follow the design recipe, every single step! ;; (Also: this is very similar to `string-times`, except that ;; we use "image arithmetic" rather than "string arithmetic".)