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:29

>>24
Optimized Racket version:

(define-syntax-parameter break
  (lambda (stx)
    (raise-syntax-error 'break "not in a loop")))
(define-syntax-parameter continue
  (lambda (stx)
    (raise-syntax-error 'continue "not in a loop")))

(define-syntax-rule (let-stxparam ((x y) ...) . body)
  (syntax-parameterize ((x (syntax-rules () ((x) (y)))) ...)
    . body))
(define-syntax-rule (let/ec-stxparam x . body)
  (let/ec k
    (let-stxparam ((x k)) . body)))

(define-syntax-rule (for init cond incr . body)
  (let/ec-stxparam break
    init
    (let loop ()
      (when cond
        (let/ec-stxparam continue . body)
        incr (loop)))))
(define-syntax-rule (while cond . body)
  (for (void) cond (void) . body))

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