2014-10-03

posted Oct 3, 2014, 11:09 AM by Samuel Konstantinovich   [ updated Oct 3, 2014, 11:09 AM ]
Versions of military time:

(define (militaryTime h ampm)
  (cond
    ( (and (= h 12) (= ampm 0))  0 )
    ( (and (= h 12) (= ampm 1))  12)
    ( (= ampm 0)  h)
    ( else       (+ h 12))))

;check if ampm is 0 or 1, use remainder
(define (militaryTime h ampm)
  (if (= ampm 0)
      (remainder h 12)
      (+ 12 (remainder h 12)) ))

;check if 12 or not, use the fact that ampm is 0 or 1
(define (militaryTime h ampm)
  (if (not (= h 12))
      (+ h (* 12 ampm))
      (* ampm 12)))

;use the remainder AND also use the fact that ampm is 0 or 1
(define (militaryTime h ampm)
      (+ (remainder h 12) (* 12 ampm)))
  

Version of leapyear:

(define (leapyear year)
   (or
       (and 
         (not (= 0(remainder year 100))) 
         (= 0 (remainder year 4)))
       (= 0(remainder year 400))))
  

(leapyear 1900)
(leapyear 2000)
(leapyear 2001)
(leapyear 2004)

Comments