Nautical bell: Difference between revisions

added Racket
(Cf. Sleep)
(added Racket)
Line 446:
23:00, First watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇
23:30, First watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ </pre>
 
=={{header|Racket}}==
 
This solution uses local time. It also doesn't poll. If it's printing to a terminal
that displays \a by playing the system bell, it will play the system bell.
 
<lang racket>#lang racket
 
(require racket/date)
 
(define HALF-HOUR-SECS (* 60 30))
 
;; given a date, return the seconds corresponding to the beginning
;; of that day (in local time)
(define (beginning-of-date d)
(find-seconds 0 0 0 (date-day d) (date-month d) (date-year d)))
 
;; the seconds at the beginning of today:
(define today-secs
(beginning-of-date
(seconds->date (current-seconds))))
 
;; hours -> watch : given an hour, return the watch name
(define (hours->watch hours)
(cond [(= 0 hours) "first"]
[(< 0 hours 4.5) "middle"]
[(< 4 hours 8.5) "morning"]
[(< 8 hours 12.5) "forenoon"]
[(< 12 hours 16.5) "afternoon"]
[(< 16 hours 20.5) "dog"]
[(< 20 hours 24.5) "first"]))
 
;; wait until current-seconds is the given number
(define (wait-til secs)
(sleep (- secs (current-seconds))))
 
;; display the appropriate message
(define (format-and-print hours bells)
(define int-hours (floor hours))
(define minutes (cond [(integer? hours) "00"]
[else "30"]))
(display
(~a
(~a (floor hours) #:min-width 2 #:pad-string "0"
#:align 'right)
":" minutes ", " bells " bell(s) of the "
(hours->watch hours) " watch "))
;; play the bells, if possible:
(for ([i bells])
(display "\a♪")
(flush-output)
(cond [(even? i) (sleep 0.5)]
[(odd? i) (display " ") (sleep 1)]))
(display "\n"))
 
;; start the loop:
(for ([s (in-range today-secs +inf.0 HALF-HOUR-SECS)]
[bells (sequence-tail (in-cycle (in-range 8)) 7)]
[hours (in-cycle (in-range 0 24 1/2))])
;; ignore the ones that have already happened:
(when (< (current-seconds) s)
(wait-til s)
(format-and-print hours (add1 bells))))</lang racket>
 
This might produce the following output:
 
<pre>
21:00, 2 bell(s) of the first watch �♪�♪
21:30, 3 bell(s) of the first watch �♪�♪ �♪
22:00, 4 bell(s) of the first watch �♪�♪ �♪�♪
22:30, 5 bell(s) of the first watch �♪�♪ �♪�♪ �♪
23:00, 6 bell(s) of the first watch �♪�♪ �♪�♪ �♪�♪
23:30, 7 bell(s) of the first watch �♪�♪ �♪�♪ �♪�♪ �♪
00:00, 8 bell(s) of the first watch �♪�♪ �♪�♪ �♪�♪ �♪�♪
00:30, 1 bell(s) of the middle watch �♪
01:00, 2 bell(s) of the middle watch �♪�♪
</pre>
 
 
=={{header|REXX}}==
Anonymous user