Subscribed unsubscribe Subscribe Subscribe

Clojureでscanl

Clojure

lazy-seqはこういうときに使えるのか。

user> (defn scanl [f q ls]                                                                               
        (lazy-seq                                                                                        
          (cons q                                                                                        
                (if (empty? ls)                                                                          
                    '()                                                                                  
                    (scanl f (f q (first ls)) (rest ls))))))                                             
#'user/scanl                                                                                             
user> (scanl + 0 (range 0 9))                                                                            
(0 0 1 3 6 10 15 21 28 36)                                                                               
user> (take 10 (scanl + 0 (iterate inc 0)))
(0 0 1 3 6 10 15 21 28 36)