Typed racket , GUI, fahrenheit converter

In the list of esoteric languages, racket scheme has his place.

cat explicit.rkt
Code:
#lang typed/racket/gui
(require typed/racket/class)      ; Adds 'new', 'send', 'get-field', etc.

;; Define the main dialog window
(define dialog : (Instance Dialog%)
  (new dialog% [label "Temperature Converter"]))

;; Input field for the numeric value
(define textField : (Instance Text-Field%)
  (new text-field% [parent dialog] [label "Enter value: "]))

;; Label to display the result
(define output : (Instance Message%)
  (new message% [parent dialog] [label "Result will appear here"] [auto-resize #t]))

;; Container for the controls
(define panel : (Instance Horizontal-Panel%)
  (new horizontal-panel% [parent dialog] [alignment '(center center)]))

;; Checkbox to determine direction (True = Fahrenheit)
(define check : (Instance Check-Box%)
  (new check-box% [parent panel] [label "to Fahrenheit"]))

;; Calculate button with explicit callback types
(define calc-button : (Instance Button%)
  (new button% [parent panel]
       [label "Calculate"]
       [callback (lambda ([button : (Instance Button%)] [event : (Instance Control-Event%)])
                   (let* ([val-str : String (send textField get-value)]
                          [num : (Option Number) (string->number val-str)]
                          [to-f? : Boolean (send check get-value)])
                 
                     (cond
                       [(not num)
                        (send output set-label "Invalid Input")]
                       [to-f?
                        ;; Celsius to Fahrenheit: (C * 9/5) + 32
                        (let ([f : Number (+ (* num (/ 9 5.0)) 32)])
                          (send output set-label (number->string (real-part f))))]
                       [else
                        ;; Fahrenheit to Celsius: (F - 32) * 5/9
                        (let ([c : Number (* (- num 32) (/ 5 9.0))])
                          (send output set-label (number->string (real-part c))))])))]))

;; Display the GUI
(send dialog show #t)

to build & run:
Code:
#!/bin/sh
set -e
rm ./explicit
rm -vfR ./build
echo "make"
raco make -j 6 explicit.rkt
echo "exe"
raco exe explicit.rkt
echo "distribute"
raco distribute ./build ./explicit
echo "done"
./build/bin/explicit

test.png
 
I just use a small script I wrote years ago, because I'd look up the temperature and my wife is more familiar with Celcius. So, I'd look up the temperature in Farenheit, from a local site.
Then
#!/bin/sh echo "Enter Farenheit temperature" read ftemp ctemp=$(echo "scale=1; ($ftemp - 32.0) * 5.0 / 9.0" |bc) echo $ctemp exit 0

One can use /usr/bin/units But I always forget /usr/share/misc/definitions.units file's definition of the two. (It's degF and degC if anyone's interested).
 
5 is 9
10 is 18
0 is 32.
100 is 212 but start at 0 or 32

5C is (32+9)F.
Why over complicate it? -5C is (32 -9)F
20C is (32+2*18)F=68F

9/5 is 1.8 which is "double - 20%" all kind of trivial without paper.

-40 is fun because -40C is -40F.

Unless you are baking where temp is kind of important, just figure out cold, comfortable, hot, really cold.
For me 20C is upper end of comfortable, 8C lower end of shorts weather. 30C is way too effin hot. -10C is "dogs love this and I need to wear a hat"
 
Back
Top