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 18:21

>>32
And try-catch-finally-style exceptions:
And this, >>23, is why macros and continuations are important: I, alone, in less than one hour, have written what takes ages in other languages.

(define exception-handler
  (make-parameter (lambda (x) (error "uncaught exception:" x))))

(define (throw x)
  ((exception-handler) x))

(define-syntax try
  (syntax-rules (catch else finally)
    ((try body ... (catch exception (predicate . pbody) ...) (finally . fbody))
     (let* ((finally (lambda () . fbody))
            (old-throw (exception-handler))
            (handler (lambda (exception)
                       (let ((x (cond ((predicate exception) . pbody) ...
                                      (else (finally) (old-throw exception)))))
                         (finally) x))))
       (let/ec throw-cont
         (let ((x (parameterize ((exception-handler (lambda (x) (throw-cont (handler x)))))
                    body ...)))
           (finally) x))))
    ((try body ... (catch exception (predicate . pbody) ...))
     (try body ... (catch exception (predicate . pbody) ...) (finally (void))))
    ((try body ... (finally . fbody))
     (try body ... (catch e) (finally . fbody)))))

(try
 (try
  2
  (throw 3)
  4
  (catch e
    (string? (display "not here")))
  (finally (display "finally")))
 4
 (catch e
   (number? (display " here") e)))

; output: finally here
; returns: 3

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