There is also a faster way to calculate fibonacci numbers in Clojure
(code taken from from
http://en.wikibooks.org/wiki/Clojure_Programming#Lazy_Fibonacci):

(defn fib-seq []
  ((fn rfib [a b]
       (lazy-cons a (rfib b (+ a b))))
    0 1))

user=> (time (take 38 (fib-seq)))
"Elapsed time: 0.032965 msecs"
(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
10946 17711 28657 46368 75025 121393 196418 317811 514229 832040
1346269 2178309 3524578 5702887 9227465 14930352 24157817)

Lauri


2008/10/19 [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
>
> Clojure's
>
> (defn fib [n]
>   (if  (or (zero? n) (= n 1))
>       1
>      (+  (fib (dec n) )  (fib (- n 2)))))
>
> (time (fib 36))
>
> "Elapsed Time:  10475.325226 msecs"
> 24157817
>
> Scala's
>
> def fib(n:Int):Int=if (n==0||n==1)1 else fib(n-1)+fib(n-2)
> def time(cal: =>Int)={
>  val beginTime=System.currentTimeMillis
>  cal
>  val endTime=System.currentTimeMillis
>  println(endTime-beginTime)
> }
> fib(36)
> res70:Int=24157817
> time(fib(36))
> 263
>
> Both not tail recursive,both running on Repl (scala's interpreter),but
> the difference between two is huge
> 10475~263
> My box : Intel core2 cpu 1.86G,2G mem
> Clojure and scala both latest build from svn
>
> any ideas?
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to