Jump to content

Programming Fundamentals/Strings/Clojure

From Wikiversity

strings.clj

[edit | edit source]
;; This program splits a given comma-separated name into first and last name 
;; components and then displays the name.
;;
;; Reference:
;;     https://www.mathsisfun.com/temperature-conversion.html

(use '[clojure.string])

(defn get-name []
  (println "Enter name (last, first):")
  (read-line)) 

(defn get-last [full-name]
  (let [index (index-of full-name ",")]
    (if (< index 0) "" (subs full-name 0 index))))

(defn get-first [full-name]
  (let [index (index-of full-name ",")]
    (if (< index 0) "" (trim (subs full-name (+ index 1))))))

(defn display-name [first-name last-name]
  (println (str "\nHello, " first-name " " last-name "!")))

(defn -main []
  (let [full-name (get-name)
        first-name (get-first full-name)
        last-name (get-last full-name)]
        (display-name first-name last-name)))

(-main)

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]