-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
c0c720e
commit 639cf76
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, " % "!") |