Subscribed unsubscribe Subscribe Subscribe

ClojureでmapAccumL

Clojure

こっちはスタックオーバフローする。lazy-seq意味なし。

user> (defn map-accum-l [f s ls]                                                                         
        (if (empty? ls)                                                                                  
            [s []]                                                                                       
            (let [[ss y]   (f s (first ls))                                                              
                  [sss ys] (map-accum-l f ss (rest ls))]                                                 
              [sss (lazy-seq (cons y ys))])))
#'user/map-accum-l                                                                                       
user> (map-accum-l (fn [x y] [x (* x y)]) 5 [9 6 3])
[5 (45 30 15)]  
user> (take 10 (first (rest (map-accum-l (fn [x y] [x (* x y)]) 5 (iterate inc 0)))))
; Evaluation aborted.

Haskellでは

Prelude Data.List> take 10 $ snd $ mapAccumL (\x y -> (x,x*y)) 5 [0..]
[0,5,10,15,20,25,30,35,40,45]

参考: Haskell : mapAccumL