Board logo

标题: Scheme 函数式编程:工具箱 [打印本页]

作者: 老刘1号    时间: 2024-3-16 21:43     标题: Scheme 函数式编程:工具箱

《函数程序设计算法》读书笔记。

列表映射
  1. (define square (lambda (x) (* x x)))
复制代码
  1. (define (sum-of-squares . nums)
  2.   (apply + (map square nums)))
复制代码
  1. (sum-of-squares 3 4)
复制代码
  1. 25
复制代码
  1. (define f (lambda (x) (+ x 1)))
复制代码
  1. (map f (list 1 2 3 4 5))
复制代码
  1. (2 3 4 5 6)
复制代码
  1. (define (sum-of-cubes . nums)
  2.   (apply + (map * nums nums nums)))
复制代码
  1. (sum-of-cubes 1 2 3)
复制代码
  1. 36
复制代码
  1. (define (sum-of-cubes . nums)
  2.   (apply + (map (lambda (nums)
  3.                   (* nums nums nums))
  4.                 nums)))
复制代码
  1. (sum-of-cubes 1 2 3)
复制代码
  1. 36
复制代码
常量过程
  1. (define (values? . ignored)
  2.   #t)
复制代码
  1. (values? 1)
复制代码
  1. #t
复制代码
  1. (define (constant v) (lambda ignored v))
复制代码
  1. (define hey-kid (constant "Why?"))
复制代码
  1. (hey-kid "Don't put your gum in the electrical outlet.")
复制代码
  1. "Why?"
复制代码
  1. (hey-kid "It's gross, and you'll get a shock.")
复制代码
  1. "Why?"
复制代码
  1. (hey-kid "The gum is wet. There's an electrical current.")
复制代码
  1. "Why?"
复制代码
  1. (hey-kid "Just don't do it. okay?")
复制代码
  1. "Why?"
复制代码
  1. (define len (lambda (ls)
  2.   (apply + (map (constant 1) ls))))
复制代码
  1. (len (list 3 2 3 4 7))
复制代码
  1. 5
复制代码
  1. (length (list 3 2 3 4 4)) ; built-in
复制代码
  1. 5
复制代码
过程节选
  1. (define (invoke procedure . args)
  2.   (apply procedure args))
复制代码
  1. (invoke + 1 2 3)
复制代码
  1. 6
复制代码
  1. (define power-of-two
  2.   (lambda (power) (expt 2 power)))
复制代码
  1. (power-of-two 10)
复制代码
  1. 1024.0
复制代码
  1. (define (curry procedure)
  2.   (lambda (initial)
  3.     (lambda remaining
  4.       (apply procedure
  5.              (append (list initial) remaining)))))
复制代码
  1. (define equal-to? (curry equal?))
复制代码
  1. (equal-to? 2)
复制代码
  1. #<procedure>
复制代码
  1. ((equal-to? 2) (+ 1 3))
复制代码
  1. #f
复制代码
  1. ((equal-to? 2) (+ 1 1))
复制代码
  1. #t
复制代码
耦合器
  1. (define (compose outer inner)
  2.   (lambda args
  3.     (let ((intermediates (apply inner args)))
  4.       (apply outer (list intermediates)))))
复制代码
  1. (define (pipe earlier later)
  2.   (lambda args
  3.     (let ((intermediates (apply earlier args)))
  4.       (apply later (list intermediates)))))
复制代码
  1. (pipe + power-of-two)
复制代码
  1. #<procedure>
复制代码
  1. ((pipe + power-of-two) 3 5)
复制代码
  1. 256.0
复制代码
  1. ((compose power-of-two +) 3 5)
复制代码
  1. 256.0
复制代码
  1. (define (cross . procedures)
  2.   (lambda args
  3.     (map invoke procedures args)))
复制代码
  1. (define add1 ((curry +) 1))
  2. (define sub1 ((curry +) -1))
  3. (define transfer-unit (cross sub1 add1))
复制代码
  1. (transfer-unit 861 19)
复制代码
  1. (860 20)
复制代码
  1. (define (sect1 f x)
  2.   (lambda (y)
  3.     (f x y)))
  4. (define (sect2 f y)
  5.   (lambda (x)
  6.     (f x y)))
  7. (define (dispatch . procedures)
  8.   (lambda args
  9.     (map (sect2 apply args) procedures)))
复制代码
  1. ((dispatch + *) 3 4)
复制代码
  1. (7 12)
复制代码
  1. (define (unwrap-apply f) (lambda (args) (apply f args)))
复制代码
  1. ((unwrap-apply +) (list 2 3 4))
复制代码
  1. 9
复制代码
  1. ((pipe (dispatch + *) (unwrap-apply <)) 3 4)
复制代码
  1. #t
复制代码
  1. ((pipe (dispatch + *) (unwrap-apply <)) 1 2)
复制代码
  1. #f
复制代码
适配器
  1. (define (>initial initial . ignored)
  2.   initial)
复制代码
  1. (define (>next initial next . ignored)
  2.   next)
复制代码
  1. (>initial 1 2 3) ; > means 'keep'
复制代码
  1. 1
复制代码
  1. (>initial 0 #t '())
复制代码
  1. 0
复制代码
  1. (transfer-unit 861 19)
复制代码
  1. (860 20)
复制代码
  1. ((pipe transfer-unit (unwrap-apply >initial)) 861 19)
复制代码
  1. 860
复制代码
  1. (define (>all-but-initial initial . others) others)
复制代码
  1. ((pipe transfer-unit (unwrap-apply >all-but-initial)) 861 19)
复制代码
  1. (20)
复制代码
  1. (define (identity something) something)
复制代码
  1. (identity 2333)
复制代码
  1. 2333
复制代码
  1. (define (>exch initial next . others)
  2.   (append (list next initial) others))
复制代码
  1. (>exch 1 2)
复制代码
  1. (2 1)
复制代码
  1. (define (echo . args) (display args))
复制代码
  1. (define (converse f) (pipe >exch (unwrap-apply f)))
复制代码
  1. (expt 3 5)
复制代码
  1. 243.0
复制代码
  1. ((converse expt) 3 5)
复制代码
  1. 125.0
复制代码
  1. (define (~initial procedure)
  2.   (lambda (initial . others)
  3.     (cons (procedure initial) others)))
复制代码
  1. (~initial (sect2 * 3))
复制代码
  1. #<procedure>
复制代码
  1. ((~initial (sect2 * 3)) 3 4 5 6)
复制代码
  1. (9 4 5 6)
复制代码
  1. (define (~next procedure)
  2.   (lambda (initial next . others)
  3.     (cons initial (cons (procedure next) others))))
复制代码
  1. (~next (sect2 * 3))
复制代码
  1. #<procedure>
复制代码
  1. ((~next (sect2 * 3)) 3 4 5 6)
复制代码
  1. (3 12 5 6)
复制代码
  1. (define (~each f)
  2.   (lambda args
  3.     (map f args)))
复制代码
  1. (~each (sect2 * 3))
复制代码
  1. #<procedure>
复制代码
  1. ((~each (sect2 * 3)) 3 4 5 6)
复制代码
  1. (9 12 15 18)
复制代码
  1. (define sum-of-squares (pipe (~each square) (unwrap-apply +)))
复制代码
  1. (sum-of-squares 3 4 5 6)
复制代码
  1. 86
复制代码
  1. (define (compare-by pre comparer)
  2.   (pipe (~each pre) (unwrap-apply comparer)))
复制代码
递归管理器
  1. (define (recur base? terminal simplify integrate)
  2.   (define (recurrer guide)
  3.     (if (base? guide)
  4.         (terminal guide)
  5.         (let* ((res (simplify guide))
  6.                (current (car res))
  7.                (next (cadr res))
  8.                (recursive-results (recurrer next)))
  9.           (apply integrate (list current recursive-results)))))
  10.   recurrer)
复制代码
  1. ((dispatch identity sub1) 3)
复制代码
  1. (3 2)
复制代码
  1. (define factorial (recur zero? (constant 1) (dispatch identity sub1) *))
复制代码
  1. (factorial 5)
复制代码
  1. 120
复制代码
  1. (define (build base? terminal derive simplify integrate)
  2.   (define (builder . guides)
  3.     (if (apply base? guides)
  4.         (apply terminal guides)
  5.         (let* ((recursive-results
  6.                 (apply (pipe simplify (unwrap-apply builder)) guides)))
  7.           (apply integrate (list (apply derive guides) recursive-results)))))
  8.   builder)
复制代码
  1. (define (wrap . args) args)
复制代码
  1. (define factorial2
  2. (build (lambda (a b)
  3.          (and (<= a 1) (<= b 1)))
  4.        (constant (list 1 1))
  5.        wrap
  6.        (lambda (a b) (list (max (sub1 a) 1) (max (sub1 b) 1)))
  7.        (lambda (x y) (list (* (car x) (car y)) (* (cadr x) (cadr y))))))
复制代码
  1. (factorial2 3 5)
复制代码
  1. (6 120)
复制代码
  1. (factorial2 7 2)
复制代码
  1. (5040 2)
复制代码
  1. (factorial2 0 0)
复制代码
  1. (1 1)
复制代码
  1. (factorial2 1 0)
复制代码
  1. (1 1)
复制代码
  1. (null? '())
复制代码
  1. #t
复制代码
  1. (sum (list 3 4))
复制代码
  1. 7
复制代码
  1. (length (list 3 4))
复制代码
  1. 2
复制代码
  1. (define (wrap . args) args)
  2. (define arithmetic-mean (pipe (pipe wrap (dispatch sum length)) (unwrap-apply /)))
复制代码
  1. (arithmetic-mean 3 5)
复制代码
  1. 4
复制代码
  1. (arithmetic-mean 1 2 3)
复制代码
  1. 2
复制代码
  1. (arithmetic-mean 1 10 100)
复制代码
  1. 37
复制代码
  1. (arithmetic-mean 1 2 1.5)
复制代码
  1. 1.5
复制代码
  1. (constant 1)
复制代码
  1. #<procedure>
复制代码
  1. ((constant 1))
复制代码
  1. 1
复制代码
  1. ((constant 1) 'others)
复制代码
  1. 1
复制代码
  1. (length (list))
复制代码
  1. 0
复制代码
  1. (define halve (sect2 div 2))
复制代码
  1. (halve 20)
复制代码
  1. 10
复制代码
  1. (halve 5)
复制代码
  1. 2
复制代码
  1. (define (power-of-two? candidate)
  2.   (or (= candidate 1)
  3.       (and (even? candidate)
  4.            (power-of-two? (halve candidate)))))
复制代码
  1. (power-of-two? 2048)
复制代码
  1. #t
复制代码
  1. (power-of-two? 4860)
复制代码
  1. #f
复制代码
  1. (define (check stop? continue? step)
  2.   (define (checker . args)
  3.     (or (apply stop? args)
  4.         (and (apply continue? args)
  5.              (apply (pipe step checker) args))))
  6.   checker)
复制代码
  1. (define power-of-two? (check (sect2 = 1) even? halve))
复制代码
  1. (power-of-two? 2048)
复制代码
  1. #t
复制代码
  1. (power-of-two? 4860)
复制代码
  1. #f
复制代码
  1. (define (iterate stop? step)
  2.   (define (iterator . args)
  3.     (if (apply stop? args)
  4.         args
  5.         (apply (pipe step iterator) args)))
  6.   iterator)
复制代码
  1. (define greatest-odd-divisor (iterate odd? halve))
复制代码
  1. (greatest-odd-divisor 24)
复制代码
  1. (3)
复制代码
  1. (define double (sect1 * 2))
复制代码
  1. (((lambda (bound)
  2.   (iterate (unwrap-apply (pipe >initial (sect2 >= bound)))
  3.            (unwrap-apply (cross double add1)))) 23) '(1 0))
复制代码
  1. ((32 5))
复制代码
  1. (define (ceiling-of-log-two bound)
  2.   ((pipe (iterate (unwrap-apply (pipe >initial (sect2 >= bound)))
  3.            (unwrap-apply (cross double add1)))
  4.          (unwrap-apply (unwrap-apply >next)))
  5.    '(1 0)))
复制代码
  1. (ceiling-of-log-two 23)
复制代码
  1. 5
复制代码
  1. (ceiling-of-log-two 32)
复制代码
  1. 5
复制代码
  1. (ceiling-of-log-two 8)
复制代码
  1. 3
复制代码
  1. (ceiling-of-log-two 34)
复制代码
  1. 6
复制代码
  1. (define (ceiling-of-log-two bound)
  2.   (define (doubler most-recent-double count)
  3.     (if (>= most-recent-double bound)
  4.         count
  5.         (doubler (double most-recent-double) (add1 count))))
  6.   (doubler 1 0))
复制代码
  1. (ceiling-of-log-two 34)
复制代码
  1. 6
复制代码
辗转相除法
  1. mod
复制代码
  1. #<procedure>
复制代码
  1. (define divisible-by? (pipe mod zero?))
复制代码
  1. (divisible-by? 60 2)
复制代码
  1. #t
复制代码
  1. (divisible-by? 60 7)
复制代码
  1. #f
复制代码
  1. (define lesser (lambda (x y) (if (< x y) x y)))
复制代码
  1. (define (greatest-common-divisor left right) ; brute-force
  2.   (let ((divides-both? (lambda (candidate)
  3.                          (and (divisible-by? left candidate)
  4.                               (divisible-by? right candidate)))))
  5.         ((iterate divides-both? sub1) (lesser left right))))
复制代码
  1. (greatest-common-divisor 20 12)
复制代码
  1. (4)
复制代码
  1. ((dispatch >next mod) 3 4)
复制代码
  1. (4 3)
复制代码
  1. ((lambda (arg) ((dispatch >next mod) (car arg) (cadr arg))) '(12 20))
复制代码
  1. (20 12)
复制代码
  1. ((iterate (unwrap-apply divisible-by?)
  2.            (unwrap-apply (dispatch >next mod)))
  3.            '(20 12))
复制代码
  1. ((8 4))
复制代码
  1. (define (greater-and-lesser l r)
  2.   (if (< l r)
  3.       (list r l)
  4.       (list l r)))
复制代码
  1. (greater-and-lesser 3 4)
复制代码
  1. (4 3)
复制代码
  1. (greater-and-lesser 4 3)
复制代码
  1. (4 3)
复制代码
  1. (greater-and-lesser 0 0)
复制代码
  1. (0 0)
复制代码
  1. (define greatest-common-divisor
  2.   (pipe greater-and-lesser
  3.         (pipe  (iterate (unwrap-apply divisible-by?)
  4.            (unwrap-apply (dispatch >next mod)))
  5.                             (unwrap-apply (unwrap-apply >next)))))
复制代码
  1. (greatest-common-divisor 12 20)
复制代码
  1. 4
复制代码
  1. (greatest-common-divisor 120 270)
复制代码
  1. 30
复制代码
  1. (greatest-common-divisor 270 120)
复制代码
  1. 30
复制代码
高阶布尔过程
  1. (define (^not condition-met?)
  2.   (pipe condition-met? not))
复制代码
  1. ((^not zero?) 3)
复制代码
  1. #t
复制代码
  1. ((^not zero?) 0)
复制代码
  1. #f
复制代码
  1. ((^not zero?) 0)
复制代码
  1. #f
复制代码
  1. (define (^et left-condition-met? right-condition-met?)
  2.   (lambda args
  3.     (and (apply left-condition-met? args)
  4.          (apply right-condition-met? args))))
复制代码
  1. ((^et number? even?) 3)
复制代码
  1. #f
复制代码
  1. ((^et zero? even?) 4)
复制代码
  1. #f
复制代码
  1. ((^et zero? even?) 0)
复制代码
  1. #t
复制代码
  1. (define (^vel left-condition-met? right-condition-met?)
  2.   (lambda args
  3.     (or (apply left-condition-met? args)
  4.         (apply right-condition-met? args))))
复制代码
  1. ((^vel zero? odd?) 0)
复制代码
  1. #t
复制代码
  1. ((^vel zero? odd?) 1)
复制代码
  1. #t
复制代码
  1. ((^vel zero? odd?) 2)
复制代码
  1. #f
复制代码
  1. (define (^if condition-met? consequent alternate)
  2.   (lambda args
  3.     (if (apply condition-met? args)
  4.         (apply consequent args)
  5.         (apply alternate args))))
复制代码
  1. (define disparity (^if < (converse -) -))
复制代码
  1. (disparity 588 920)
复制代码
  1. 332
复制代码
  1. (disparity 920 588)
复制代码
  1. 332
复制代码
  1. (define (conditionally-combine combine? combiner)
  2.   (lambda (initial . others)
  3.     (if (combine? initial)
  4.         (list (apply combiner (cons initial others)))
  5.         others)))
复制代码
  1. ((conditionally-combine odd? +) 1 2)
复制代码
  1. (3)
复制代码
  1. ((conditionally-combine odd? +) 2 2)
复制代码
  1. (2)
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2