This is what I had in mind:
(define (stream-foldr fun acc str)
(if (stream-empty? str)
acc
(fun (stream-foldr fun acc (stream-rest str))
(stream-first str))))
(define (inverterd-cons acc e) (cons e acc))
(stream-foldr inverterd-cons '() (stream 1 2 3 4 5))
=> '(1 2 3 4 5)
but it works for one valued streams only.
Hmm, reusing some of the code from before, perhaps something such as this?
(define (n-val-stream . ss)
(if (ormap stream-empty? ss)
empty-stream
(stream-cons (apply values (map stream-first ss))
(apply n-val-stream (map stream-rest ss)))))
(define (stream-foldr fun acc str)
(if (stream-empty? str)
acc
(call-with-values
(thunk (stream-first str))
(curry fun (stream-foldr fun acc (stream-rest str))))))
(define s1 (stream 1 2 3 4 5))
(define s2 (stream 1 2 3 4 5))
(define (inverterd-cons acc . es) (cons es acc))
(stream-foldr inverterd-cons '() (n-val-stream s1 s2))
;=> '((1 1) (2 2) (3 3) (4 4) (5 5))
Edit: ormap
might be safer.
1 Like
This is what i found:
(define-syntax values->list
(syntax-rules ()
[(_ (values v ...))
(call-with-values
(lambda () (values v ...))
(lambda v v))]))
(define (stream-foldr fun acc mvstr)
(if (stream-empty? mvstr)
acc
(apply fun (append (values->list
(stream-foldr fun acc (stream-rest mvstr)))
(values->list (stream-first mvstr))))))
(define (inv-cons-mul acc e1 e2) (cons (* e1 e2) acc))
(stream-foldr inv-cons-mul '() (stream (values 1 10)
(values 2 100)
(values 3 1000)))
;=> '(10 200 3000)
a bit more complicated.
1 Like