Programming Fundamentals/Functions/Clojure

From Wikiversity
Jump to navigation Jump to search

functions.clj[edit | edit source]

;; This program asks the user for a Fahrenheit temperature,
;; converts the given temperature to Celsius,
;; and displays the results.
;; References:

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

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

(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))


Try It[edit | edit source]

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 | edit source]