[ZOIS] Home Page * Contact ZOIS * Technical Notes

A Scheme Based Clock

ZOIS Technical Note TN-2006-06-01.

Author and Audience

A small clock program is presented in Scheme as an exemplar. Written by Martin Sullivan[au], ZOIS Limited, Cockermouth.

Abstract

A small Scheme program to display a clock on a Handheld is published as an exemplar.

Introduction

The author is in the habit of writing programs which display an analogue clock. He tends to do this as an intellectual exercise when returning to a programming environment that he was once familiar with. Scheme, a dialect of LISP, is no exception and the rather simple, elegant result is documented here as an adjunct to the somewhat clunkier examples written in other language systems[jc, cc].

Materials and Platform

At the time a Palm Tungsten E[pt] was being used extensively. Although by the standards of the day (2006) it was already an elderly platform, the author was delighted to discover that it had a fully working Scheme interpreter on it, together with graphics and TCP/IP connectivity[lm]. If one wanted to rapidly write a program on this platform, here was the environment to do it.

Method

The clock program, once written, was rather a small piece of code and it is presented here in its entirety:

; A real clock, again

(define (clock) 
    (set! *gstate* (default-gstate))
  (while #t  (run-clock) (pwait 60))
)

(define (run-clock) 
        (cls)
    (do ((ix 0 (+ 1 ix)))  ((> ix 11) #n )(draw-radial-line 
        (* (/ 360 12) ix) 0.9 1.0))
        (draw-radial-line (+ 
        (/ (* (minute) 360) 60) 
        (/ (/ (* (second) 360) 60) 60)) 0.0 0.7)
        (draw-radial-line (+ 
        (/ (* (hour) 360) 12) 
        (/ (/ (* (minute) 360) 60) 12)) 0.0 0.5)
 
) ; run-clock

(define (second)
        (ts-second (current-ts))
)

(define (minute)
        (time-minute (ts->time (current-ts)))
)

(define (hour)
        (time-hour (ts->time (current-ts)))
)

(define (draw-radial-line angle start stop)
    (let* (
        (two-pi 6.283)
        (max-length 50)
        (sin-angle (sin (/ (* two-pi angle) 360)))
        (cos-angle (cos (/ (* two-pi angle) 360)))
         )    
        (move (inexact->exact (+ max-length (* sin-angle 
            (* start max-length))))
                    (inexact->exact (+ max-length (* cos-angle 
                (* start (- max-length))))))
        (draw (inexact->exact (+ max-length 
            (* sin-angle (* stop max-length))))
                    (inexact->exact (+ max-length (* cos-angle 
                (* stop (- max-length))))))
    ) ; let*
) ; draw-radial-line

; In later versions of this system pwait doesn't seem to work. As a
; work-around we'll use wait-pen. The punter is going to have to tap
; the screen to get the time.
(define (pwait secs) (wait-pen))

Discussion

A small program has been written in Scheme. Obvious variations on it have been used to demonstrate tail-recursion and function decomposition, but the absolute simplest version has been presented here. It is somewhat more elegant, in the authors opinion, than other clock examples he has written in Java and 'C' (and it has to be said, unpublished examples in SmallTalk and 'C++' too).

If you are intrigued by this programming language (or indeed the art of programming in general) then read "Structure and Interpretation of Computer Programs", also known as the "Wizard Book" or simply SICP. It is now handily on-line[si] and is a good place to start.

References

References found in this section, and in particular the HTML links were correct at time of writing (2010-01-03).

[au]. Martin Sullivan:
http://www.zois.co.uk/people/martin_sullivan
[jc]. Writing a Java Program for a Mobile Phone:
http://www.zois.co.uk/tn/tn-2009-10-01.html
[cc]. A Curses Based Clock:
http://www.zois.co.uk/tn/tn-1991-01-01.html
[si]. Ableson H, Sussman JG & Sussman J (1996) "Structure and Interpretation of Computer Programs", The MIT Press Cambridge, Massachusetts:
http://mitpress.mit.edu/sicp/full-text/book/book.html
[pt]. Palm Tungsten (handheld):
http://en.wikipedia.org/wiki/Tungsten_%28handheld%29
[lm]. Byer F (2008) "LispMe":
http://www.lispme.de/lispme/index.html

$Date: 2010/01/03 17:53:51 $


Break Frame * E-mail Webmaster * Copyright