PasteRack.org
Paste # 54806
2022-03-17 21:56:58

Fork as a new paste.

Paste viewed 547 times.


Embed:

Exploring continuation speed

  1. #lang racket
  2. (define (repeat what times [result '()])
  3.   (cond [(<= times 0) (reverse result)]
  4.         [else (repeat what (sub1 times) (cons what result))]))
  5.  
  6. (define (find-first predicate? things)
  7.   (call-with-current-continuation (lambda (got-it)
  8.                                     (letrec ([check-next (lambda (remaining)
  9.                                                            (let ([one-to-check (car remaining)])
  10.                                                              (cond [(null? remaining) (got-it '())]
  11.                                                                  [(predicate? one-to-check) (got-it one-to-check)]
  12.                                                                  [else (check-next (cdr remaining))])))])
  13.                                       (check-next things)))))
  14. (define long-range (append (repeat 2 1000000) '(2 4 5 6)))
  15. (time (find-first odd? long-range))
  16. (time (findf odd? long-range))

=>

cpu time: 4 real time: 4 gc time: 0

5

cpu time: 4 real time: 4 gc time: 0

5