-
-
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.
A chance to use the function definitions we learnt in exercise #14
- Loading branch information
1 parent
53d671d
commit 6a612be
Showing
1 changed file
with
58 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,58 @@ | ||
(ns four-clojure.015-double-down) | ||
|
||
;; #015 Double down | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
;; Solutions | ||
;; Difficulty: Elementary | ||
;; Topics: | ||
|
||
;; Write a function which doubles a number. | ||
|
||
;; (= (__ 2) 4) | ||
;; (= (__ 3) 6) | ||
;; (= (__ 11) 22) | ||
;; (= (__ 7) 14) | ||
|
||
|
||
;; Deconstruct the problem | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
;; A chance to use the function definitions we have just learnt | ||
|
||
;; we need to write a function that takes a single argument | ||
|
||
(fn [arg]) | ||
|
||
;; The behaviour of the function is to double the value of the argument, | ||
;; so we can do this by multiplying by 2 | ||
|
||
(fn [arg] | ||
(* arg 2)) | ||
|
||
;; Now we can call this definition with the 4Clojure tests | ||
|
||
((fn [arg] | ||
(* arg 2)) | ||
2) | ||
;; => 4 | ||
|
||
;; and this works for all the tests. | ||
|
||
;; we can also write the function definition using the short form | ||
#(* % 2) | ||
|
||
;; and call the short form with the same test data | ||
(#(* % 2) 2) | ||
;; => 4 | ||
|
||
|
||
;; Answers summary | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
;; Normal function definition | ||
(fn [arg] | ||
(* arg 2)) | ||
|
||
;; short form function definition | ||
#(* % 2) |