fibonacci sequence using lazy-cat

2009-07-29 Thread swaroop belur
Hello all, Just started learning clojure recently - initial examples were easy to understand, until I found this example fibonacci sequence using lazy-cat : (def fibs (lazy-cat [0 1] (map + fibs (rest fibs I am trying to understand how this works ..not sure i fully comprehend it. Can

Re: fibonacci sequence using lazy-cat

2009-07-29 Thread Lauri Pesonen
Hi swaroop, 2009/7/29 swaroop belur swaroop.be...@gmail.com: fibonacci sequence using lazy-cat : (def fibs (lazy-cat [0 1]   (map + fibs (rest fibs I am trying to understand how this works ..not sure i fully comprehend it. Can anyone please explain how clojure evaluates this. I'll

Re: fibonacci sequence using lazy-cat

2009-07-29 Thread Baishampayan Ghose
Swaroop, Just started learning clojure recently - initial examples were easy to understand, until I found this example fibonacci sequence using lazy-cat : (def fibs (lazy-cat [0 1] (map + fibs (rest fibs I am trying to understand how this works ..not sure i fully comprehend

Re: fibonacci sequence using lazy-cat

2009-07-29 Thread swaroop belur
cool - thanks guys for the detailed reply. crystal clear now -:) Thx swaroop --~--~-~--~~~---~--~~ 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 Note that

Re: fibonacci sequence using lazy-cat

2009-07-29 Thread Stuart Halloway
While fibs is a nice small example, it is not idiomatic Clojure. Pointing the fibs var to the head of the list keeps the whole list in memory as it realizes. Better is to expose fibs as a *function* return the sequence, so the head is not retained. (defn fibo [] (map first (iterate (fn

Re: Fibonacci sequence

2008-12-29 Thread Craig Marshall
Hi David, (defn fib (fib-helper 0 1)) ; This doesn't work either: (defn fib (fib- helper '(0 1))) You're missing your argument list for fib. I assumed you meant the empty square brackets [] just after the work fib? I didn't realise these were necessary even when there were no function

Re: Fibonacci sequence

2008-12-29 Thread Paul Barry
Craig, Something you should be aware of is that this implementation of Fibonacci is very inefficient. For more info as to why, you can read: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.2 The short story is doing it this way performs a lot of wasted calculations as n

Fibonacci sequence

2008-12-28 Thread Craig Marshall
Hi, I'm trying to learn lisp and Clojure and so I'm trying some excercises suggested here on the group a while ago. I'm not doing too well, I had to cheat on excercise one (make a series of integers) by looking at the source code to the range function, and I'm now having to ask for help with

Re: Fibonacci sequence

2008-12-28 Thread David Nolen
(defn fib-helper [a b] (fib-helper b (+ a b))) (defn fib (fib-helper 0 1)) ; This doesn't work either: (defn fib (fib- helper '(0 1))) You're missing your argument list for fib. (println (take 5 fib)) take creates a lazy list from a collection: (take n collection) Your fib does not

Re: Fibonacci sequence

2008-12-28 Thread Michael Wood
On Sun, Dec 28, 2008 at 6:01 PM, Craig Marshall cra...@gmail.com wrote: Hi, I'm trying to learn lisp and Clojure and so I'm trying some excercises suggested here on the group a while ago. I'm not doing too well, I had to cheat on excercise one (make a series of integers) by looking at the