diff --git a/src/cljlab/flowcontrol.clj b/src/cljlab/flowcontrol.clj index ee6030e..aa747f1 100644 --- a/src/cljlab/flowcontrol.clj +++ b/src/cljlab/flowcontrol.clj @@ -100,7 +100,7 @@ ;; ** ;;; ## `when` ;;; -;;; `when` is a one-armed `if`. It checks a condition and then evaluates any number of statements as a body (so no `do` is required). The value of the last expression is returned. If the condition is false, nil is returned. +;;; `when` is a one-armed `if`. It checks a condition and then evaluates any number of expressions as a body (so no `do` is required). The value of the last expression is returned. If the condition is false, nil is returned. ;;; ;;; `when` communicates to a reader that there is no "else" branch. ;; ** @@ -514,7 +514,7 @@ ;; ** ;;; ## Euclid's algorithm -;;; [Euclid's algorithm](http://en.wikipedia.org/wiki/Euclidean_algorithm) finds the greatest common divisor of two integers using only substraction. In imperative pseudo-code, it looks like this: +;;; [Euclid's algorithm](http://en.wikipedia.org/wiki/Euclidean_algorithm) finds the greatest common divisor of two integers using only subtraction. In imperative pseudo-code, it looks like this: ;;; ;;; ``` ;;; function gcd(A, B): @@ -522,7 +522,7 @@ ;;; if A == 0 ;;; return B ;;; if B == 0 -;;; return B +;;; return A ;;; if A > B ;;; A := A - B ;;; else diff --git a/src/cljlab/functions.clj b/src/cljlab/functions.clj index 6d92576..df7ae8e 100644 --- a/src/cljlab/functions.clj +++ b/src/cljlab/functions.clj @@ -386,6 +386,9 @@ vector ;; @@ (defn do-nothing [x] ___) + +(assert (= 42 (do-nothing 42))) +(assert (= "wtf"(do-nothing "wtf"))) ;; @@ ;; ** @@ -404,6 +407,10 @@ vector ;; @@ (defn always-thing [__] ___) + +(assert (= :thing (always-thing))) +(assert (= :thing (always-thing 42))) +(assert (= :thing (always-thing "wtf" 42))) ;; @@ ;; ** @@ -515,6 +522,8 @@ vector ;; @@ (defn http-get [url] ___) + +(assert (.contains (http-get "http://www.w3.org") "html")) ;; @@ ;; ** @@ -531,6 +540,9 @@ vector ;; @@ (defn one-less-arg [f x] (fn [& args] ___)) + +(defn add-two-numbers [x y] (+ x y)) +(assert (= 3 ((one-less-arg add-two-numbers 1) 2))) ;; @@ ;; ** @@ -546,8 +558,13 @@ vector ;; @@ (defn two-fns [f g] ___) -;; @@ +(defn add-two [x] (+ 2 x)) +(defn times-ten [x] (* 10 x)) + +(assert (= 12 ((two-fns add-two times-ten) 1))) +(assert (= 30 ((two-fns times-ten add-two) 1))) +;; @@ ;; ** ;;; # Lab Solutions ;;; diff --git a/src/cljlab/polymorphism.clj b/src/cljlab/polymorphism.clj index 79ddcba..4da9187 100644 --- a/src/cljlab/polymorphism.clj +++ b/src/cljlab/polymorphism.clj @@ -9,7 +9,7 @@ ;; ** ;;; ## Motivation ;;; -;;; There are many situations where it is useful to provide behavior that varies based on type or value. Clojure provides two different mechanisms to for conditional (polymorphic) behavior: protocols and multimethods. +;;; There are many situations where it is useful to provide behavior that varies based on type or value. Clojure provides two different mechanisms for conditional (polymorphic) behavior: protocols and multimethods. ;;; ;;; Also, Clojure provides two constructs that allow you to create new data "types": records (which we saw in collections earlier) and types. ;;;