clojure-koans/src/koans/about_functions.clj

22 lines
513 B
Clojure
Raw Normal View History

2010-05-07 02:56:43 +00:00
(defn raise-to-the-tenth-power [n]
(Math/pow n 10))
2010-02-09 04:57:08 +00:00
(meditations
"Functions are often defined before they are used"
2010-05-07 02:56:43 +00:00
(= __ (raise-to-the-tenth-power 2))
2010-02-09 04:57:08 +00:00
"But they can also be defined inline"
2010-05-07 02:56:43 +00:00
(= __ ((fn [n] (Math/pow n __)) 2))
2010-02-09 04:57:08 +00:00
"Or using even shorter syntax"
2010-05-07 02:56:43 +00:00
(= __ (#(Math/pow % 10) __))
2010-02-09 04:57:08 +00:00
2010-05-07 02:56:43 +00:00
"One function can beget another"
2010-02-09 04:57:08 +00:00
(= __ (((fn []
2010-05-07 02:56:43 +00:00
(fn [a b] __ a b)))
2010-02-09 04:57:08 +00:00
4 5))
"Higher-order functions take function arguments"
(= 25 ((fn [f] (f 5))
(fn [n] (__ __ __)))))