;; 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-advanced-reader.ss" "lang")((modname 2015-11-30-lerner) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f () #f))) ;; BOOLEANS (over-eager!) (define TRUE (λ(x) (λ(y) x))) (define FALSE (λ(x) (λ(y) y))) (define IF (λ(c) (λ(t) (λ(f) ((c t) f))))) ;; LAZY BOOLEANS (because laziness is sometimes a virtue) ;; **calls** the t or f arguments it gets, as functions of zero arguments ;; But we don't have to change the definitions of TRUE or FALSE, ;; just how we call IF. ;; NOTE: In order to have functions with zero arguments, ;; we have to bump up to Advanced Student Language...finally! (define LAZY-IF (λ(c) (λ(t) (λ(f) (((c t) f)))))) (define (BOOL->bool b) ((b true) false)) (check-expect (((IF TRUE) 5) 6) 5) (check-expect (((IF FALSE) 5) 6) 6) ;; PAIRS (define PAIR (λ(x) (λ(y) (λ(s) ((s x) y))))) (define FST (λ(p) (p (λ(x) (λ(y) x))))) (define SND (λ(p) (p (λ(x) (λ(y) y))))) (check-expect (FST ((PAIR 4) 5)) 4) (check-expect (SND ((PAIR 4) 5)) 5) ;; NUMBERS (define ZERO (λ(f) (λ(x) x))) (define ONE (λ(f) (λ(x) (f ((ZERO f) x))))) (define TWO (λ(f) (λ(x) (f ((ONE f) x))))) (define THREE (λ(f) (λ(x) (f ((TWO f) x))))) (define FOUR (λ(f) (λ(x) (f ((THREE f) x))))) (define FIVE (λ(f) (λ(x) (f ((FOUR f) x))))) (define (NUM->num n) ((n add1) 0)) (check-expect (NUM->num ZERO) 0) (check-expect (NUM->num ONE) 1) (check-expect (NUM->num TWO) 2) (check-expect (NUM->num THREE) 3) (check-expect (NUM->num FOUR) 4) (check-expect (NUM->num FIVE) 5) (define ADD1 (λ(n) (λ(f) (λ(x) (f ((n f) x)))))) (check-expect (NUM->num (ADD1 FOUR)) 5) (define ADD (λ(m) (λ(n) ((m ADD1) n)))) (check-expect (NUM->num ((ADD TWO) THREE)) (NUM->num FIVE)) (define MUL (λ(m) (λ(n) ((m (ADD n)) ZERO)))) (check-expect (NUM->num ((MUL TWO) THREE)) (NUM->num (ADD1 FIVE))) (define SUB1 (local ((define (row->row r) ((PAIR (ADD1 (FST r))) (FST r)))) (λ(n) (SND ((n row->row) ((PAIR ZERO) ZERO)))))) (check-expect (NUM->num (SUB1 FIVE)) 4) (define SUB (λ(m) (λ(n) ((n SUB1) m)))) (check-expect (NUM->num ((SUB FIVE) THREE)) (NUM->num TWO)) (check-expect (NUM->num ((SUB THREE) FIVE)) (NUM->num ZERO)) (define ZERO? (λ(n) ((n (λ(dont-care-at-all) FALSE)) TRUE))) (check-expect (BOOL->bool (ZERO? ZERO)) true) (check-expect (BOOL->bool (ZERO? ONE)) false) (define (FACT n) (((LAZY-IF (ZERO? n)) (λ() ONE)) (λ() ((MUL n) (FACT (SUB1 n)))))) (check-expect (NUM->num (FACT ZERO)) 1) (check-expect (NUM->num (FACT FIVE)) 120)