[racket] in praise of if's mandatory else clause

From: Pierpaolo Bernardi (olopierpa at gmail.com)
Date: Tue May 31 08:55:03 EDT 2011

On Tue, May 31, 2011 at 07:07, Richard Cleis <rcleis at mac.com> wrote:
> Buggy loops can also be avoided by reducing the problem.  These leap-year cases are less troubling if the function that really matters is used: the days in a given year.
>
> (define (days-in-year year) (if (= 0 (remainder year 4)) 366 365))

Please.  I understand that it is just an example, and that you most
certainly know the correct definition of a leap year, but someone might
not know it and blindly copy this function somewhere where it can cause
harm.

Kids, use this one instead:

(define (leap-year? year)
  (and (zero? (modulo year 4))
       (or (not (zero? (modulo year 100)))
           (zero? (modulo year 400)))))

(define (days-in-year year)
  (if (leap-year? year)
    366
    365))


8^)

P.



Posted on the users mailing list.