Programming Fundamentals/Functions/Clojure

functions.clj

edit
;; This program asks the user for a Fahrenheit temperature,
;; converts the given temperature to Celsius,
;; and displays the results.
;;
;; References:
;;   https://www.mathsisfun.com/temperature-conversion.html

(defn get-fahrenheit []
  (println "Enter Fahrenheit temperature:")
  (let [fahrenheit (read-line)]
    (-> fahrenheit
      (format "%.2f")
      (Float/parseFloat))))

(defn calculate-celsius [fahrenheit]
  (let [celsius
    (-> fahrenheit
    (- 32)
    (* 5)
    (/ 9))]
    celsius))

(defn display-result [fahrenheit celsius]
  (println (str fahrenheit "° Fahrenheit is " celsius "° Celsius.")))

(defn main []
  (def fahrenheit (get-fahrenheit))
  (def celsius (calculate-celsius fahrenheit))
  (display-result fahrenheit celsius))

(main)

Try It

edit

Copy and paste the code above into one of the following free online development environments or use your own Clojure compiler / interpreter / IDE.

See Also

edit