;; 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-beginner-reader.ss" "lang")((modname 10-naturals) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ; A Nat is one of: ; - (make-Zero) ; - (make-Succ Nat) ; Interpretation: ; - (make-Zero) stands for the number 0 ; - (make-Succ n) stands for one more than the number n stands for (define-struct Zero ()) (define-struct Succ (pred)) (define n0 (make-Zero)) (define n1 (make-Succ n0)) (define n2 (make-Succ n1)) (define n3 (make-Succ n2)) (define n4 (make-Succ n3)) (define n5 (make-Succ n4)) #; (define (process-nat a-nat ...) (cond [(Zero? a-nat) ...] [else ... (process-nat (Succ-pred a-nat) ...) ...])) ; nat-+ : Nat Nat -> Nat ; Adds two naturals. ; ; Strategy: struct. decomp. (define (nat-+ n k) (cond [(Zero? n) k] [else (make-Succ (nat-+ (Succ-pred n) k))])) (check-expect (nat-+ n0 n0) n0) (check-expect (nat-+ n0 n2) n2) (check-expect (nat-+ n2 n0) n2) (check-expect (nat-+ n3 n2) n5) ; nat-* : Nat Nat -> Nat ; Multiplies two naturals. ; ; Strategy: struct. decomp. (define (nat-* n k) (cond [(Zero? n) (make-Zero)] [else (nat-+ k (nat-* (Succ-pred n) k))])) (check-expect (nat-* n0 n0) n0) (check-expect (nat-* n0 n2) n0) (check-expect (nat-* n2 n0) n0) (check-expect (nat-* n1 n3) n3) (check-expect (nat-* n3 n1) n3) (check-expect (nat-* n3 n2) (make-Succ n5)) ; nat->number : Nat -> Number ; Interprets a Nat as a BSL Number. ; ; Strategy: struct. decomp. (define (nat->number n) (cond [(Zero? n) 0] [else (add1 (nat->number (Succ-pred n)))])) (check-expect (nat->number n0) 0) (check-expect (nat->number n1) 1) (check-expect (nat->number n2) 2) (check-expect (nat->number n5) 5) ; nat-! : Nat -> Nat ; Computes factorial of a natural number. ; ; Strategy: struct. decomp. (define (nat-! n) (cond [(Zero? n) n1] [else (nat-* n (nat-! (Succ-pred n)))])) ;; if n is 4, then we have 3 * 2 * 1, want 4 * 3 * 2 * 1 ;; if n is 6, then we have 5 * 4 * 3 * 2 * 1, want 6 * 5 * 4 * 3 * 2 * 1 ;; if n is 1, then we have 1, want 1 * 1 ;; if we have n! = n * (n - 1)! (check-expect (nat-! n0) n1) (check-expect (nat-! n1) n1) (check-expect (nat-! n2) n2) (check-expect (nat-! n3) (nat-* n3 n2)) (check-expect (nat-! n4) (nat-* n4 (nat-* n3 n2))) ;; BSL Naturals: ; A Natural is one of: ; - 0 ; - (add1 Natural) ;; Think of natural numbers as a structure, with: ;; * constructors: 0 and add1 ;; * predicates: zero? and positive? ;; * selectors: sub1 #; (define (process-natural n ...) (cond [(zero? n) ...] [else ... (process-natural (sub1 n) ...) ...])) ; fact : Natural -> Natural ; Finds the factorial of a natural number. ; ; Strategy: struct. decomp. (define (fact n) (cond [(zero? n) 1] [else (* n (fact (sub1 n)))])) (check-expect (fact 0) 1) (check-expect (fact 1) 1) (check-expect (fact 2) 2) (check-expect (fact 3) 6) (check-expect (fact 4) 24) ; A LoN is one of: ; - '() ; - (cons Natural LoN) ; iota : Natural -> LoN ; Counts down from a number. ; ; Strategy: struct. decomp. (define (iota n) (cond [(zero? n) empty] [else (cons n (iota (sub1 n)))])) (check-expect (iota 3) (list 3 2 1)) (check-expect (iota 2) (list 2 1)) (check-expect (iota 1) (list 1)) (check-expect (iota 0) empty) ; repeat : Natural String -> String ; Copies a string some number of times. ; ; Strategy: struct. decomp. (define (repeat n str) (cond [(zero? n) ""] [else (string-append str (repeat (sub1 n) str))])) (check-expect (repeat 1 "Hello") "Hello") (check-expect (repeat 3 "Hello") "HelloHelloHello") (check-expect (repeat 0 "Hello") "")