Created
April 11, 2013 20:37
-
-
Save david-hodgetts/5366970 to your computer and use it in GitHub Desktop.
4clojure #62 (with olange)
This file contains hidden or 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
;; Take 1 (noter que lazy-cat retourne toujours une liste; | |
;; et que son implémentation utilise lazy-seq) | |
(defn iter1 [f x] (lazy-cat [x] (iter f (f x) ) ) ) | |
(= (take 5 (iter1 #(* 2 %) 1)) | |
[1 2 4 8 16]) | |
(= (take 100 (iter1 inc 0)) | |
(take 100 (range))) | |
(= (take 9 (iter1 #(inc (mod % 3)) 1)) | |
(take 9 (cycle [1 2 3]))) | |
;; Take 2 (implémentation actuelle de iterate en Clojure) | |
(defn iter2 [f x] (cons x (lazy-seq (iter f (f x) ) ) ) ) | |
(= (take 5 (iter2 #(* 2 %) 1)) | |
[1 2 4 8 16]) | |
(= (take 100 (iter2 inc 0)) | |
(take 100 (range))) | |
(= (take 9 (iter2 #(inc (mod % 3)) 1)) | |
(take 9 (cycle [1 2 3]))) | |
;; Stack overflow | |
;; (defn iter [f x] (concat [x] [ (iter f (f x)) ] ) ) | |
(= (take 5 (iter1 #(* 2 %) 1)) [1 2 4 8 16]) | |
(= (take 5 (iter2 #(* 2 %) 1)) [1 2 4 8 16]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment