Problem 4: Non Decreasing (100 pts)
Define a function nondecrease
, which takes in a scheme stream of numbers and outputs a stream of lists, which overall has the same numbers in the same order, but grouped into lists that are non-decreasing.
For example, if the input is a stream containing elements
(1 2 3 4 1 2 3 4 1 1 1 2 1 1 0 4 3 2 1)
the output should contain elements
((1 2 3 4) (1 2 3 4) (1 1 1 2) (1 1) (0 4) (3) (2) (1))
Your solution may handle infinite streams correctly, which means if an infinite streams is always non-decreasing after the n
th element, and from the 0
th to the n - 1
th element can group to m
non-decreasing sublists, your solution can output the first m
sublists correctly.
(define (nondecrease s)
'YOUR-CODE-HERE
)
;;; Tests
; scm> (define s (list-to-stream '(1 2 3 4 1 2 3 4 1 1 1 2 1 1 0 4 3 2 1))) ; a helper function to make stream from list
; s
; scm> (slice (nondecrease s) 0 8)
; ((1 2 3 4) (1 2 3 4) (1 1 1 2) (1 1) (0 4) (3) (2) (1))