Re: What is this function called?

2009-01-02 Thread Chouser

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))

That's very similar to tails in Haskell:
http://www.zvon.org/other/haskell/Outputlist/tails_f.html

 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))

For this particular example, you could use 'partition':
(take 10 (partition 5 1 (iterate inc 1)))

 Does this fn already exist in clojure?  If not what would an idiomatic name
 be for it from Haskell or CL?

I don't think it exists in clojure.core or clojure.contrib.

--Chouser

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: What is this function called?

2009-01-02 Thread Graham Fawcett

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
-~--~~~~--~~--~--~---



Re: What is this function called?

2009-01-02 Thread Perry Trolard

I did something similar using (iterate rest coll), which I called iter-
rest:

(defn iter-rest
   Takes the first (count coll) items from call to (iterate rest
coll).
   If passed function as first argument, calls it on each invocation
of
   rest, i.e. (iterate #(func (rest %)) coll).
   ([coll] (take (count coll) (iterate rest coll)))
   ([func coll] (take (count coll) (iterate #(func (rest %)) coll

user= (iter-rest (range 3))
((0 1 2) (1 2) (2))

I now realize a key difference is that, by using (count coll), iter-
rest fully realizes anything lazy; for Andrew's lazy version above, I
second the name rests.

Perry
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: What is this function called?

2009-01-02 Thread Andrew Baine
On Fri, Jan 2, 2009 at 8:09 AM, Perry Trolard trol...@gmail.com wrote:


 I did something similar using (iterate rest coll), which I called iter-
 rest:

 (defn iter-rest
   Takes the first (count coll) items from call to (iterate rest
 coll).
   If passed function as first argument, calls it on each invocation
 of
   rest, i.e. (iterate #(func (rest %)) coll).
   ([coll] (take (count coll) (iterate rest coll)))
   ([func coll] (take (count coll) (iterate #(func (rest %)) coll

 user= (iter-rest (range 3))
 ((0 1 2) (1 2) (2))

 I now realize a key difference is that, by using (count coll), iter-
 rest fully realizes anything lazy; for Andrew's lazy version above, I
 second the name rests.

 Perry
 

Okay, great, I'm using it and calling it rests.  Thanks all.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: What is this function called?

2009-01-02 Thread lpetit

I don't know if the function exists on its own in Common Lisp, but in
Common Lisp there is maplist that does what you get by combining map
and rests : http://www.lispworks.com/documentation/HyperSpec/Body/f_mapc_.htm

HTH,

--
Laurent

On Jan 2, 7:21 pm, Andrew Baine andrew.ba...@gmail.com wrote:
 On Fri, Jan 2, 2009 at 8:09 AM, Perry Trolard trol...@gmail.com wrote:

  I did something similar using (iterate rest coll), which I called iter-
  rest:

  (defn iter-rest
    Takes the first (count coll) items from call to (iterate rest
  coll).
    If passed function as first argument, calls it on each invocation
  of
    rest, i.e. (iterate #(func (rest %)) coll).
    ([coll] (take (count coll) (iterate rest coll)))
    ([func coll] (take (count coll) (iterate #(func (rest %)) coll

  user= (iter-rest (range 3))
  ((0 1 2) (1 2) (2))

  I now realize a key difference is that, by using (count coll), iter-
  rest fully realizes anything lazy; for Andrew's lazy version above, I
  second the name rests.

  Perry

 Okay, great, I'm using it and calling it rests.  Thanks all.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



What is this function called?

2009-01-01 Thread Andrew Baine
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))
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
-~--~~~~--~~--~--~---