Discussion:
[racket] TR: for/fold and #:break substitutes
Matthew Butterick
2015-03-12 21:42:41 UTC
Permalink
I have a for/fold loop that breaks on a condition related to the accumulators, not the iterators. Something like this:

(for/fold ([xs empty])([x (in-naturals)] #:break (> (length xs) 5))
(cons x xs))

I am converting it to TR. TR has no #:break, and #:when doesn't work with (in-naturals).

I notice that I can convert the break condition into a `stop-after` like so:


#lang racket
(require typed/racket rackunit)

(for/fold ([xs empty])([x (in-naturals)] #:break (> (length xs) 5))
(cons x xs))

;; '(5 4 3 2 1 0)

(with-type
#:result (Listof Integer)
(for/fold ([xs : (Listof Integer) empty])
([x (stop-after (in-naturals) (λ(x) (> (length xs) 5)))])
(cons x xs)))

;; '(5 4 3 2 1 0)


Is this an acceptable way of handling the #:break condition in TR? Is there a better way?



____________________
Racket Users list:
http

Loading...