(defn fibo
([]
(concat [0 1] (fibo 0 1)))
([a b]
(let [n (+ a b)]
(lazy-seq
(cons n (fibo b n))))))
Is there a better/more idiomatic approach, without resorting to code
in clojure-contrib? Test your code against the following expression to
flush out stack and heap overflows.
(rem (nth (fibo) 1000000) 1000)
-> 875
Also, the current 'fibs' implementation in clojure.contrib.seq fails
the test above, because it holds the entire sequence as it goes. We
should replace it with whatever the community comes up with on this
thread.
Cheers,
Stu
> Also, the current 'fibs' implementation in clojure.contrib.seq fails
> the test above, because it holds the entire sequence as it goes. We
> should replace it with whatever the community comes up with on this
> thread.
The fibs implementation in clojure.contrib.lazy-seqs is not a function
that returns fib(n) given n. Instead, it is an infinite sequence of
all the fibonacci numbers. It holds onto its head because it's
intended to. It's surely not the right way to deal with fibonacci
numbers in every context, but I don't think it needs replacing because
it requires a large java heap to pass this test:
(rem (nth clojure.contrib.lazy-seqs/fibs 1000000) 1000)
-> 875
I think defining a fib(n) function somewhere in contrib or core that
operates as efficiently as we can manage would be a good idea.
--Steve
Too bad GMP is LGPLv3+, while Java is GPLv2 only, so we'll not see
Java using GMP in the foreseeable foture. Kaffe's JVM uses GMP,
though. If Clojure runs on it, it'd be interesting to see what sort of
numbers it produces.
I've not seen a decent GMP wrapper for Java -- does anyone know of any
such implementation?
Regards,
--
miʃel salim • http://hircus.jaiku.com/
IUCS • msa...@cs.indiana.edu
Fedora • sal...@fedoraproject.org
MacPorts • hir...@macports.org
Beautiful-thanks.
- Afficher le texte des messages précédents -
(Btw, Stuart, due to latest changes in Clojure's lazy-seq
implementation, rec-seq is atom-based again.)
Christophe
Laurent PETIT a écrit :
> Hello,It would work and that's why Stuart said "without resorting to code in
> What about giving back 'lazy-cons (or lcons as a shortcut), with the
> optional possibility to give the cons an internal name ?
clojure-contrib":
(defn fibo[]
(seq-utils/rec-cat fib [0 1] (map + fib (rest fib))))
Prior to r1300 it was even easier: no change to Clojure was required.
> Could'nt it be interesting to consider adding the possibility to make
> self recursive calls inherent to lazy-seqs, as I proposed ?
While having played a lot with rec-cons, rec-cat and rec-seq — which can
prove useful in simplifying some code —, I'm still ambivalent about the
general case of recursive data structures in functional programming
that's why I'm not lobbying for their inclusion in core.
> Have you looked at the patch I provided ?
Yes: simple and clever.
Christophe