[racket] TR: for/fold and #:break substitutes

From: Matthew Butterick (mb at mbtype.com)
Date: Thu Mar 12 17:42:41 EDT 2015

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?




Posted on the users mailing list.