# A for/fold #:break question, I think

**URL:** <https://racket.discourse.group/t/a-for-fold-break-question-i-think/1553>\
**Category:** Questions & Answers\
**Created:** [December 13, 2022, 7:43am UTC](https://racket.discourse.group/t/a-for-fold-break-question-i-think/1553 "2022-12-13T07:43:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbhanson](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tbhanson/32/906_2.png) [@tbhanson](https://racket.discourse.group/u/tbhanson)\
**Post date:** [December 13, 2022, 7:43am UTC](https://racket.discourse.group/t/a-for-fold-break-question-i-think/1553/1 "2022-12-13T07:43:35Z")

</div>

hi, folks, I posted this question here: [https://groups.google.com/g/racket-users/c/LblfrTvKVx4](https://groups.google.com/g/racket-users/c/LblfrTvKVx4) yestereve, but just discovered this discourse group. I hope it's OK to cross-post here, so to speak.

--tbh

hi,

I'm trying to do something slightly harder than what the attached example tries to do, but this example suffices to show that I, evidently, am missing something about how for/fold and/or #:break work together.

(I have used both before, but have gotten out of practice with racket of late and am stuck longer on this than seems reasonable.)

I expect both tests to pass, but both fail (the "j" for jewel in "xxjxx") is seen but not "found", as it were.

I'd be grateful for a bug fix and/or explanation.

Cheers,

Tim Hanson

* * *

```scheme
#lang racket

(require rackunit)

(define (port->char-stream aport)
  (printf "(port->char-stream aport)~n")
  (define (process-next-ch-iter)
    (let ([ch (read-char aport)])
      (printf "p->cs ch: ~a~n" ch)
      (if (eof-object? ch)
          empty-stream
          (stream-cons
           ch
           (process-next-ch-iter)))))
  
  (process-next-ch-iter))

(let ([my-port
       (open-input-string "xxjxx")])
  
  (let ([ch-stream (port->char-stream my-port)])
    (let-values ([(pos-found? pos)
                  (for/fold ([j-found? #f]
                             [pos 0])
                            (#:break j-found?
                             ;#:final j-found?
                             [ch ch-stream]
                             [ch-idx (in-naturals 1)])
                    (begin
                      (printf "ch: ~a~n" ch)
                      (printf "ch-idx: ~a~n" ch-idx)
                      (printf "(eq? ch #\\j): ~a~n" (eq? ch #\j))
                      (values
                       (eq? ch #\j)
                       ch-idx)))])
      
      (check-equal? pos-found? #t)

      (check-equal? pos 3)

      (close-input-port my-port))))

```

---

<div class="post-metadata">

**Author:** ![tbhanson](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tbhanson/32/906_2.png) [@tbhanson](https://racket.discourse.group/u/tbhanson)\
**Post date:** [December 13, 2022, 10:15am UTC](https://racket.discourse.group/t/a-for-fold-break-question-i-think/1553/2 "2022-12-13T10:15:42Z")

</div>

I read docs, played with examples, and (re-)discovered, that the position of #:break or #:final in the iterator clauses (of course) matters. So this, e.g.

```
 (for/fold ([j-found? #f]
                         [pos 0])
                        
                        ([ch ch-stream]
                         [ch-idx (in-naturals 1)]
                         #:break j-found?)

```

solves my problem.

---

<div class="post-metadata">

**Author:** ![joeld](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/joeld/32/38_2.png) [@joeld](https://racket.discourse.group/u/joeld)\
**Post date:** [December 13, 2022, 6:38pm UTC](https://racket.discourse.group/t/a-for-fold-break-question-i-think/1553/3 "2022-12-13T18:38:38Z")

</div>

> [@tbhanson](#):
>
> hi, folks, I posted this question here: [https://groups.google.com/g/racket-users/c/LblfrTvKVx4](https://groups.google.com/g/racket-users/c/LblfrTvKVx4) yestereve, but just discovered this discourse group. I hope it's OK to cross-post here, so to speak.

It’s more than OK; this site is the replacement for the google group, and the google group will soon be shut down.

---

<div class="post-metadata">

**Author:** ![tbhanson](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tbhanson/32/906_2.png) [@tbhanson](https://racket.discourse.group/u/tbhanson)\
**Post date:** [February 21, 2023, 12:11pm UTC](https://racket.discourse.group/t/a-for-fold-break-question-i-think/1553/4 "2023-02-21T12:11:35Z")

</div>

Thanks, joeld. Good to know.
