You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
1.7 KiB
144 lines
1.7 KiB
|
|
Exercise 3.70: It would be nice to be able to
|
|
generate streams in which the pairs appear in some useful order, rather than in
|
|
the order that results from an ad hoc interleaving process. We can use
|
|
a technique similar to the merge procedure of Exercise 3.56, if we
|
|
define a way to say that one pair of integers is “less than” another. One
|
|
way to do this is to define a “weighting function”
|
|
|
|
W
|
|
(
|
|
i
|
|
,
|
|
j
|
|
)
|
|
|
|
and
|
|
stipulate that
|
|
|
|
(
|
|
|
|
i
|
|
1
|
|
|
|
,
|
|
|
|
j
|
|
1
|
|
|
|
)
|
|
|
|
is less than
|
|
|
|
(
|
|
|
|
i
|
|
2
|
|
|
|
,
|
|
|
|
j
|
|
2
|
|
|
|
)
|
|
|
|
if
|
|
|
|
|
|
W
|
|
(
|
|
|
|
i
|
|
1
|
|
|
|
,
|
|
|
|
j
|
|
1
|
|
|
|
)
|
|
<
|
|
|
|
|
|
W
|
|
(
|
|
|
|
i
|
|
2
|
|
|
|
,
|
|
|
|
j
|
|
2
|
|
|
|
)
|
|
|
|
. Write a procedure
|
|
merge-weighted that is like merge, except that
|
|
merge-weighted takes an additional argument weight, which is a
|
|
procedure that computes the weight of a pair, and is used to determine the
|
|
order in which elements should appear in the resulting merged
|
|
stream.197 Using this, generalize pairs to a procedure
|
|
weighted-pairs that takes two streams, together with a procedure that
|
|
computes a weighting function, and generates the stream of pairs, ordered
|
|
according to weight. Use your procedure to generate
|
|
|
|
|
|
the stream of all pairs of positive integers
|
|
|
|
(
|
|
i
|
|
,
|
|
j
|
|
)
|
|
|
|
with
|
|
|
|
i
|
|
≤
|
|
j
|
|
|
|
|
|
ordered according to the sum
|
|
|
|
i
|
|
+
|
|
j
|
|
|
|
,
|
|
|
|
the stream of all pairs of positive integers
|
|
|
|
(
|
|
i
|
|
,
|
|
j
|
|
)
|
|
|
|
with
|
|
|
|
i
|
|
≤
|
|
j
|
|
|
|
,
|
|
where neither
|
|
i
|
|
nor
|
|
j
|
|
is divisible by 2, 3, or 5, and the pairs are
|
|
ordered according to the sum
|
|
|
|
2
|
|
i
|
|
+
|
|
3
|
|
j
|
|
+
|
|
5
|
|
i
|
|
j
|
|
|
|
.
|
|
|
|
|
|
|