Hi, all
I want some list functions in Haskell like mapAccumL in
clojure.contrib, because some sequence operations are difficult with
only functions in clojure.core and contrib.
Think about writing a function 'accum-seq', which takes a sequence of
numbers, and returns a sequence of numbers. Each element of returned
sequence is sum of numbers from the beginning to its position in given
sequence.
Ex)
user> (accum-seq [1, 1, 1, 1, 1])
(1 2 3 4 5)
user> (accum-seq [1, 2, 3, 4, 5])
(1 3 6 10 15)
user> (accum-seq [1, -1, 1, -1, 1])
(1 0 1 0 1)
If you know any smart solutions with only currently available
functions, please tell me. I mean, 'smart' solutions have no explicit
'lazy-seq', recursion, and return a lazy sequence as a result.
This 'accum-seq' can be easily implemented by mapAccumL in Haskell
library. Here's my implementation of mapAccumL, and 'accum-seq' using
mapAccumL.
(defn accuml [f e coll]
(if (empty? coll)
(empty coll)
(let [pair (f e (first coll))]
(lazy-seq (cons pair (accumL f (first pair) (rest coll)))))))
(defn map-accuml [f e coll]
(let [pair-coll (accumL f e coll)]
[(first (last pair-coll)),
(map second pair-coll)]))
(defn accum-seq [coll]
(map-accuml (fn [x y] [(+ x y), (+ x y)]) 0 coll))
I searched these kind of functions in Clojure API documentations, but
I could not find what I want. I hope these Haskell-style sequence
functions are included in clojure.contrib.
Anyway, I thank all of developers and contributors for creating such a
nice language. I really love it.
----
Yuto Hayamizu
Master's degree student at Kitsuregawa Laboratory
Department of Information and Communication Engineering
Graduate School of Information Science and Technology
University of Tokyo
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
To unsubscribe, reply using "remove me" as the subject.