#!/usr/bin/env racket
#lang racket
(require srfi/1 srfi/11 srfi/13 srfi/41 srfi/48)
(define *calender-base*
(stream-map (lambda (year)
`(31 ,(if (or (zero? (modulo year 400))
(and (zero? (modulo year 4))
(not (zero? (modulo year 100)))))
29
28)
31 30 31 30 31 31 30 31 30 31))
(stream-from 0)))
(define (make-calender year month)
(let loop ((blanks (modulo
(fold +
(stream-fold + 0
(stream-map
(lambda (y)
(apply + y))
(stream-cdr
(stream-take year *calender-base*))))
(take (stream-ref *calender-base* year) (- month 1)))
7))
(count 0)
(days (iota (list-ref
(stream-ref *calender-base* year)
(- month 1)) 1))
(ls '()))
(cond ((null? days) (apply string-append (reverse ls)))
((= count 7) (loop blanks
0
days
(cons "~%" ls)))
((zero? blanks) (loop blanks
(+ count 1)
(cdr days)
(cons (string-pad
(number->string (car days))
3) ls)))
(else (loop (- blanks 1)
(+ count 1)
days
(cons " " ls))))))
(define *msg* '("西暦年を入力して下さい "
"月を入力して下さい "
"~%~%~t~d年~2F月~%"))
(define (main)
(let loop ((msg *msg*) (env '()))
(cond ((null? msg)
(format #t (make-calender (cadr env) (car env))))
((= (length msg) 1)
(format #t (car msg) (cadr env) (car env))
(loop (cdr msg) env))
(else (format #t (car msg))
(loop (cdr msg) (cons (read) env))))))
(main)