On Thu, Jan 1, 2009 at 10:14 PM, Andrew Baine <andrew.ba...@gmail.com> wrote:
> I want to get a seq of successive rests of the given seq:
> user> (defn f [seq]
> (if (empty? seq)
>  nil
>  (lazy-cons seq (f (rest seq)))))
> #'user/f
> user> (f '(1 2 3 4))
> ((1 2 3 4) (2 3 4) (3 4) (4))

Hi,

Haskell has "tails" in the Data.List module which is similar, though
it includes a null-list as a final value:

*Main> Data.List.tails [1..4]
[[1,2,3,4],[2,3,4],[3,4],[4],[]]

*Main> take 10 . map (take 5) . Data.List.tails $ iterate (+1) 1
[[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7],[4,5,6,7,8],[5,6,7,8,9],[6,7,8,9,10],[7,8,9,10,11],[8,9,10,11,12],[9,10,11,12,13],[10,11,12,13,14]]

"Rests" makes good sense as a Clojure-name, IMO, as does not including
a null-value at the end.

Graham


> user> (take 10 (map #(take 5 %) (f (iterate inc 1))))
> ((1 2 3 4 5) (2 3 4 5 6) (3 4 5 6 7) (4 5 6 7 8) (5 6 7 8 9) (6 7 8 9 10) (7
> 8 9 10 11) (8 9 10 11 12) (9 10 11 12 13) (10 11 12 13 14))
> Does this fn already exist in clojure?  If not what would an idiomatic name
> be for it from Haskell or CL?
> Andrew
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to