# Streams questions

**URL:** <https://racket.discourse.group/t/streams-questions/3147>\
**Category:** Questions & Answers\
**Created:** [September 2, 2024, 11:46am UTC](https://racket.discourse.group/t/streams-questions/3147 "2024-09-02T11:46:09Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![matteo-daddio](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/matteo-daddio/32/1245_2.png) [@matteo-daddio](https://racket.discourse.group/u/matteo-daddio)\
**Post date:** [September 9, 2024, 9:22am UTC](https://racket.discourse.group/t/streams-questions/3147/21 "2024-09-09T09:22:16Z")

</div>

This is what I had in mind:

```scheme
(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.

---

<div class="post-metadata">

**Author:** ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)\
**Post date:** [September 9, 2024, 10:43am UTC](https://racket.discourse.group/t/streams-questions/3147/22 "2024-09-09T10:43:08Z")

</div>

Hmm, reusing some of the code from before, perhaps something such as this?

```scheme
(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.

---

<div class="post-metadata">

**Author:** ![matteo-daddio](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/matteo-daddio/32/1245_2.png) [@matteo-daddio](https://racket.discourse.group/u/matteo-daddio)\
**Post date:** [September 9, 2024, 11:49am UTC](https://racket.discourse.group/t/streams-questions/3147/23 "2024-09-09T11:49:23Z")

</div>

This is what i found:

```scheme
(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.

[Previous page](https://racket.discourse.group/t/streams-questions/3147.md?page=1)
