-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtask-2.rkt
executable file
·32 lines (24 loc) · 1.24 KB
/
task-2.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#! /usr/bin/env gracket
#lang racket/gui
;; a bi-directional temperature converter (Fahrenheit vs Celsius)
(require 7GUI/Macros/7guis 7GUI/Macros/7state)
(define-syntax-rule (propagate-to state f field)
(λ (new-value-of-origin)
(set! state (stop (f new-value-of-origin)))
(send field set-field-background (make-object color% "white"))
(send field set-value (~r state #:precision 4))))
(define-state *C 0 (propagate-to *F (λ (c) (+ (* c 9/5) 32)) F-field))
(define-state *F 32 (propagate-to *C (λ (f) (* (- f 32) 5/9)) C-field))
(define (string->number* str)
(define n (string->number str))
(values n (and n (string-ref str (- (string-length str) 1)))))
(define flow
(with (values field:num last) #:post string->number*
(send self set-field-background (make-object color% "white"))
(cond
[(and field:num (rational? field:num)) (* #i1.0 field:num)]
[else (send self set-field-background (make-object color% "red")) none])))
(define temp-field% (class text-field% (super-new [min-width 200])))
(gui "Temperature Converter"
((#:id F-field temp-field% #:change *F flow [init-value "32"][label "fahrenheit:"])
(#:id C-field temp-field% #:change *C flow [init-value "0"][label "celsius:"])))