1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ;;The multiplication is saved but not executed (define my-promise (delay (* 5 6 7 8 9))) ;;Again, saved but not executed (define another-promise (delay (sqrt 9))) ;;Forces the multiplication to execute. Saves and returns the value (display (force my-promise)) (newline) ;;This time, the multiplication doesn't have to execute. It just returns ;;the value that was previously saved. (display (force my-promise)) (newline) ;;This produces an error, because the promise must be forced to be used (display (+ my-promise (force another-promise))) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | (define (square x) (* x x)) (define (calculate-statistics the-series) (let* ( (size (length the-series)) (sum (apply + the-series)) (mean (/ sum size)) ;variance is the sum of (x - mean)^2 for all list values (variance (let* ( (variance-list (map (lambda (x) (square (- x mean))) the-series))) (/ (apply + variance-list) size))) (standard-deviation (sqrt variance))) (vector mean variance standard-deviation))) ;Run the program (display (calculate-statistics '(2 6 4 3 7 4 3 6 7 8 43 4 3 2 36 75 3))) (newline) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | (define (square x) (* x x)) (define (calculate-statistics the-series) (let* ( (size (delay (length the-series))) (mean (delay (/ (apply + the-series) (force size)))) ;variance is the sum of (x - mean)^2 (variance (delay (let* ( (variance-list (map (lambda (x) (square (- x (force mean)))) the-series))) (/ (apply + variance-list) (force size))))) (standard-deviation (delay (sqrt (force variance))))) (vector mean variance standard-deviation))) ;Run the program (define stats (calculate-statistics '(2 6 4 3 7 4 3 6 7 8 43 4 3 2 36 75 3))) (define mean (force (vector-ref stats 0))) (define variance (force (vector-ref stats 1))) (define stddev (force (vector-ref stats 2))) (display (list mean variance stddev)) (newline) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class StatisticsCalculator { private List the_series; private Double mean; private Integer size; private Double variance; private Double standard_deviation; public StatisticsCalculator(List l) { the_series = l; } public int getSize() { if(size == null) { size = the_series.size(); } return size; } public double getStandardDeviation() { if(stddev == null) { stddev = Math.sqrt(getVariance()); } return stddev; } ... ... } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |