From 639cf76e7901809413eb01f9c5a2f07187764ae0 Mon Sep 17 00:00:00 2001 From: John Stevenson Date: Fri, 14 Dec 2018 23:17:18 +0000 Subject: [PATCH] 016: Hello World Using the `str` function to join values together. Writing our own functions to insert a persons name in between other strings, using both the normal `fn` function and the `#_` reader macro short form of a function definition. --- src/four_clojure/016_hello_world.clj | 87 ++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/four_clojure/016_hello_world.clj diff --git a/src/four_clojure/016_hello_world.clj b/src/four_clojure/016_hello_world.clj new file mode 100644 index 0000000..d75bb20 --- /dev/null +++ b/src/four_clojure/016_hello_world.clj @@ -0,0 +1,87 @@ +(ns four-clojure.016-hello-world) + +;; #16 Hello World +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Difficulty: Elementary + +;; Write a function which returns a personalized greeting. + +;; (= (__ "Dave") "Hello, Dave!") +;; (= (__ "Jenn") "Hello, Jenn!") +;; (= (__ "Rhea") "Hello, Rhea!") + +;; Deconstruct the problem +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Looking for a function that will join values together to form a string. + +;; Additionally we need to define a function that inserts a string inside the sentence. We cannot simply put Hello in front of the name, we need to add an exclamation mark afterwards + + +;; REPL experiments +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; The `str` function will join values and turn them into a string. + +;; Joining two strings together is straight forward. +(str "Hello," "Dave") +;; => "Hello,Dave" + +;; The `str` function does exactly what you specify. So in the previous example there is no space between the strings that are joined. + +;; Spaces need to be explicitly specified +(str "Hello, " "Dave") +;; => "Hello, Dave" + +;; or you can include spaces separately, depending on which seems more readable. + +(str "Hello," " " "Dave") +;; => "Hello, Dave" + + + +;; Define a function that takes a persons name and inserts it between Hello, and ! + +(fn [person-name] + (str "Hello, " person-name "!")) + + +;; and call that function definition with a persons name as an argument +((fn [person-name] + (str "Hello, " person-name "!")) "Dave") +;; => "Hello, Dave!" + + +;; There is a short form for defining a function, using a reader macro. + +#(str "I am a function definition using the reader macro") + +;; And to call this function definition with an argument it needs to be put inside a list, `()` +;; Argument are used by placing a percent symbol, `%`, and the argument number within the function definition +;; `%1` is the first argument passed to the function. Or you can just use `%` + +(#(str %1 ", " "I am a function definition using the reader macro. I take one argument") + "Dave") +;; => "Dave, I am a function definition using the reader macro. I take one argument" + +;; Applying this short form to the 4Clojure answer we get: + +;; using the short form of a function definition +#(str "Hello, " % "!") + +;; and the short form of calling a function +(#(str "Hello, " % "!") + "Mani") + + + +;; Answers summary +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; The normal form for a function definition is nicely readable +(fn [person-name] + (str "Hello, " person-name "!")) + +;; As this funciton is relatively simple, then even the short form is quite readable and succinct +#(str "Hello, " % "!")