1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ;This is the generative rule for the list. It returns a pair ;with the car being the next value, and the cdr being a promise ;for the next pair (define (my-generative-rule last-val) (let ( ;generative formula based on previous value (next-val (+ last-val 5))) ;put the next value together with a promise for another one (cons next-val (delay (my-generative-rule next-val))))) ;Since the cdr is a promise of a pair, rather than a pair itself, ;we have our own functions for getting the car and cdr. (define (mystream-car the-stream) (car the-stream)) (define (mystream-cdr the-stream) (force (cdr the-stream))) ;Create our list (define multiples-of-five (cons 5 (delay (my-generative-rule 5)))) ;Display the fourth element of the list (display (mystream-car (mystream-cdr (mystream-cdr (mystream-cdr multiples-of-five))))) (newline) |
1 2 3 4 5 6 7 8 9 10 11 12 | (define (mystream-map function the-stream) (cons ;;The first value will be the function applied to the car (function (car the-stream)) ;;The remaining values will be stored in a promise (delay (mystream-map function (mystream-cdr the-stream))))) (define squared-stream (mystream-map (lambda (x) (* x x)) multiples-of-five)) ;Display the fourth element of this new list (display (mystream-car (mystream-cdr (mystream-cdr (mystream-cdr squared-stream))))) (newline) |
1 2 3 4 5 6 7 8 | (let loop ( (remaining-stream squared-stream)) (if (>= (mystream-car remaining-stream) 500) #f (begin (display (mystream-car remaining-stream)) (newline) (loop (mystream-cdr remaining-stream))))) |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |