Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

A student's question

Name: Anonymous 2011-05-30 12:20

My teacher keeps drilling into our collective heads that using the break command is harmful, and that if we wish to terminate a loop early it would be far better to create a boolean variable, set it to 0, and add a condition to the head of the loop, and set the boolean variable to 1 when a break is needed. Note that this way you actually have to check this every single loop (where it is not needed almost every time) as well as waste a command to reset the boolean in case it was set to true.

Is there a reason to this?

Name: Anonymous 2011-05-30 17:35

>>26
They are all call-with-escape-continuation. That may be implemented with CL's block+return-from too (same concept).

(define-syntax for/break-at
  (syntax-rules ()
    ((_ name init cond body . rest)
     (call-with-current-continuation
      (lambda (name) (for init cond body . rest))))))
;; short one
(define-syntax-rule (for/break-at n . rest)
  (let/ec n (for . rest)))

(for/break-at il (set! ii 0) (< ii 1000) (inc! ii)
 (for/break-at jl (set! jj 0) (< jj 1000) (inc! jj)
  (for/break-at kl (set! kk 0) (< kk 1000) (inc! kk)
   (when break? (il)))))


It looks bad, though, but that's because I'm just forcing C-style for into Scheme.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List