Countdown

From MHCGraphics

Jump to: navigation, search
; Mead example file -- a countdown
;
; Jim Teresco
; Computer Science 110
; Mount Holyoke College
; 
; Borrowed from Duane A. Bailey, Williams College
;
; This is really just Scheme example - it doesn't make use
; of Mead
;
; $Id: Countdown.scm 569 2008-03-24 02:09:18Z terescoj $
;
; Here's a function that counts 10 ... 2 1 blastoff!
; It's self referential, or recursive.
(define (countdown n)
  ; if takes 3 parameters: the first is a condition, the
  ; second and third are functions (or s-expressions).
  ; if the condition is true, the function in the second
  ; parameter is executed, otherwise the function in the
  ; third parameter is executed
  (if (<= n 0)
      (print "Blastoff!") ; simple case
      (begin ; this begin wraps 3 s-exprs into 1
        (print n) ; prints the value of n (as you might guess)
        (newline) ; advance the output to the next line
        ; at this point, we've printed the value of n.  In
        ; order to complete our countdown, we need to count
        ; (n-1), (n-2), ..., 1.  Well, we have a function that
        ; can do that (which we just happen to be writing 
        ; right now), but we just go ahead and call it.
        (countdown (- n 1)) ; recursion happens here
        )
      )
  )
Personal tools