ListsLinked Lists - SICP Comparison Edition"/>
In section 3.5.1, we showed how to
implement streams as delayed listslinked lists.
We introduced special forms delay and
cons-stream, which allowed us
We used a
lambda expression
to construct a
promise
to compute the
cdr
tail
of a stream, without actually fulfilling that promise until later.
| Original | Python | |
| We could use this general technique of introducing special forms whenever we need more control over the evaluation process, but this is awkward. For one thing, a special form is not a first-class object like a procedure, so we cannot use it together with higher-order procedures.[1] Additionally, we were forced to create streams as a new kind of data object similar but not identical to lists, and this required us to reimplement many ordinary list operations (map, append, and so on) for use with streams. | We were forced to create streams as a new kind of data object similar but not identical to linked lists, and this required us to reimplement many ordinary linked list operations (map, append, and so on) for use with streams. |
With lazy evaluation, streams and listslinked lists can be identical, so there is no need for special forms or for separate listlinked list and stream operations. All we need to do is to arrange matters so that cons pair is non-strict. One way to accomplish this is to extend the lazy evaluator to allow for non-strict built-in functions, and to implement cons pair as one of these. An easier way is to recall (section 2.1.3) that there is no fundamental need to implement cons pair as a built-in function at all. Instead, we can represent pairs as proceduresfunctions:[2]
| Original | Python |
| (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) (define (cdr z) (z (lambda (p q) q))) | def pair(x, y): return lambda m: m(x, y) def head(z): return z(lambda p, q: p) def tail(z): return z(lambda p, q: q) |
In terms of these basic operations, the standard definitions of the listlinked list operations will work with infinite listslinked lists (streams) as well as finite ones, and the stream operations can be implemented as listlinked list operations. Here are some examples:
| Original | Python |
| (define (list-ref items n) (if (= n 0) (car items) (list-ref (cdr items) (- n 1)))) (define (map proc items) (if (null? items) '() (cons (proc (car items)) (map proc (cdr items))))) (define (scale-list items factor) (map (lambda (x) (* x factor)) items)) (define (add-lists list1 list2) (cond ((null? list1) list2) ((null? list2) list1) (else (cons (+ (car list1) (car list2)) (add-lists (cdr list1) (cdr list2)))))) (define ones (cons 1 ones)) (define integers (cons 1 (add-lists ones integers))) | def llist_ref(items, n): return head(items) if n == 0 else llist_ref(tail(items), n - 1) def map(fun, items): if is_none(items): return None else: return pair(fun(head(items)), map(fun, tail(items))) def scale_llist(items, factor): return map(lambda x: x * factor, items) def add_llists(list1, list2): if is_none(list1): return list2 elif is_none(list2): return list1 else: return pair(head(list1) + head(list2), add_llists(tail(list1), tail(list2))) ones = pair(1, ones) integers = pair(1, add_llists(ones, integers)) |
| Original | Python |
| ;;; L-Eval input: (list-ref integers 17) ;;; L-Eval value: 18 | L-evaluate input: print(llist_ref(integers, 17)) 18 |
Note that these lazy listslinked lists are even lazier than the streams of chapter 3: The car head of the listlinked list, as well as the cdr, tail, is delayed.[3] In fact, even accessing the car head or cdr tail of a lazy pair need not force the value of a listlinked list element. The value will be forced only when it is really needed—e.g., for use as the argument of a built-in function, or to be printed as an answer.
Lazy pairs also help with the problem that arose with streams in section 3.5.4, where we found that formulating stream models of systems with loops may require us to sprinkle our programs with explicit delay operations, beyond the ones supplied by cons-stream. additional lambda expressions for delays, beyond the ones required to construct a stream pair. With lazy evaluation, all arguments to procedures functions are delayed uniformly. For instance, we can implement procedures functions to integrate listslinked lists and solve differential equations as we originally intended in section 3.5.4:
| Original | Python |
| (define (integral integrand initial-value dt) (define int (cons initial-value (add-lists (scale-list integrand dt) int))) int) (define (solve f y0 dt) (define y (integral dy y0 dt)) (define dy (map f y)) y) ;;; L-Eval input: (list-ref (solve (lambda (x) x) 1 0.001) 1000) ;;; L-Eval value: 2.716924 | def integral(integrand, initial_value, dt): int = pair(initial_value, add_llists(scale_llist(integrand, dt), int)) return int def solve(f, y0, dt): y = integral(dy, y0, dt) dy = map(f, y) return y |
lazierlazy listslinked lists described in this section. How can you take advantage of this extra laziness?
| Original | Python |
| (car '(a b c)) | head(llist('a', 'b', 'c')) |
listslinked listsobtained by reading in quoted expressions from the built-in llist function are different from the listslinked lists manipulated by the new definitions of cons, pair, car, head, and cdr. tail. Modify the evaluator's treatment of quoted expressions so that quoted lists the evaluator such that applications of the built-in llist function typed at the driver loop will produce true lazy listslinked lists.
lazy trees.