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"
 
A friend had a system to do it in her head. Cx2+30=F. F-30 divided by 20=C. It's a rough estimate and off by a few degrees, but works fine for temperatures around 20 to 80 F, which in NYC is usually what we deal with. For example 20 F is -6.6 C. So 20- 30 is -10, divided by 2 is -5. So about a degree off.
 
A friend had a system to do it in her head. Cx2+30=F. F-30 divided by 20=C. It's a rough estimate and off by a few degrees, but works fine for temperatures around 20 to 80 F, which in NYC is usually what we deal with. For example 20 F is -6.6 C. So 20- 30 is -10, divided by 2 is -5. So about a degree off.
Exactly. That's similar to my "10 is 18" 10C change is 18F change start at 0C is 32F. In terms of human comfort 5F (or about 2.5C) is needed for a feeling of change, but in baking +/- 2F may be a lot. Quick estimates of the fractions is (used to be) trivial math.
 
A friend had a system to do it in her head. Cx2+30=F. F-30 divided by 20=C. It's a rough estimate and off by a few degrees, but works fine for temperatures around 20 to 80 F, which in NYC is usually what we deal with. For example 20 F is -6.6 C. So 20- 30 is -10, divided by 2 is -5. So about a degree off.
Never tried it. What goes wrong if you maintain 1C = 1.8F and assume an amount from 0 like a currency? Not too difficult between boiling and freezing points.
On Mi to km, imperial wins on unit size this time. 😆
 
You need to use the units from TheRegister, like double decker busses, brontosaurus, hedgehogs per fortnight, speed of an unmolested sheep in vacuum, ...
And can we toss these imperials and farenheits onto the midden of history? They make no sense.
 
Oh no, someone is wrong on the internet. The unit name uses a lowercase character (degrees fahrenheit), the unit symbol uses a capital letter if the unit is named after a person (°F). By the way, you can do it all with shell arithmetic expansion, without the aide of bc(1)/dc(1):​
Bash:
#!/bin/sh -eu
[ -t 0 ] && printf 'Enter temperature in (whole) degrees fahrenheit: '
read -r reading
[ "${reading}" -ge -459 ] || exit 1
C=$(((reading - 32) * 500 / 9))
printf '% 4d°F  ≈  % 6.1f°C\n' \
	"${reading}" \
	$((C / 100)).$((C % 100 * (C < 0 ? -1 : +1)))
 
Oh no, someone is wrong on the internet. The unit name uses a lowercase character (degrees fahrenheit), the unit symbol uses a capital letter if the unit is named after a person (°F). By the way, you can do it all with shell arithmetic expansion, without the aide of bc(1)/dc(1):​
Bash:
#!/bin/sh -eu
[ -t 0 ] && printf 'Enter temperature in (whole) degrees fahrenheit: '
read -r reading
[ "${reading}" -ge -459 ] || exit 1
C=$(((reading - 32) * 500 / 9))
printf '% 4d°F  ≈  % 6.1f°C\n' \
    "${reading}" \
    $((C / 100)).$((C % 100 * (C < 0 ? -1 : +1)))
Interesting , must have a deeper look. But it's bash.
 
[…] but units uses degF and degC, upper case, […]
Err, yeah? The temperature scale degrees celsius is named after Anders Celsius so the symbol uses a capital C. The length unit meter uses a lowercase m, since there was no “Mr. or Mrs. Meter”.​
[…] But it's bash.
Only for the purposes of syntax highlighting. Unfortunately, [code=sh] doesn’t look good, hence [code=bash].​
 
Back
Top