Hi Meikel,
It seems to me that your version is the only safe one so far, that
would succesfully indefinitely return values with this test:
(dorun (mystery-function true? :foo (repeat true)))
Mine, a new version of mine I'll never bother to publish, and
Christophe's all retain head.
To explain on Christophe's one for example:
It uses (split-with) which, in case pred always match coll elements,
will retain the head of coll in etc, while eating more and more
elements of coll via running on run :
1:21 user=> (defn mystery-function [pred coll]
(lazy-seq
(when (seq coll)
(let [[run etc] (split-with pred coll)]
(if (seq run)
(concat run (cons :foo (mystery-function pred etc)))
(cons (first coll) (mystery-function pred (rest coll))))))))
1:22 user=> (dorun (mystery-function true? (repeat true)))
java.lang.OutOfMemoryError: GC overhead limit exceeded (repl-1:22)
HTH,
--
Laurent
2009/4/22 Meikel Brandmeyer <[email protected]>:
> Hi Micheal,
>
> Am 22.04.2009 um 21:18 schrieb Michael Wood:
>
>> Your version gives the same answer as mine, but I believe what he
>> wants is something that skips over all the elements that pass the test
>> and only inserts one instance of o after them. That's why in his
>> example there is not a :foo after 1, but only after 5.
>>
>> Laurent's version gets this right.
>
> Oops. Sorry, misread the message. Another try.
>
> (defn mystery-function
> [f o s]
> (let [step (fn step [b s]
> (lazy-seq
> (if-let [s (seq s)]
> (let [fst (first s)]
> (cond
> (and (f fst) b) (cons fst (step b (rest s)))
> b (cons o (step false s))
> :else (cons fst (step (f fst) (rest s)))))
> (when b (list o)))))]
> (step false s)))
>
> Sincerely
> Meikel
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---