From: J. I. Gyasu on
Ken Tilton wrote:
>
>
> J. I. Gyasu wrote:
>> After a bit of effort, my first working lisp code which is slightly
>> more complex than printing "hello", it returns the nth fibonacci number.
>> How would you lisp gurus have written the code in the proper lisp way.
>
> Meaning that it works so our teacher will accept it?
>

You clearly know more about me than I know about myself. :)

> Cool, your homework solution fails on the first fibonnaci number. That
> was the only one I got right.

While the program might be wrong, it did not given any wrong answer for
any n>=1, I checked for.


Here is my clisp session:

[1]> (defun fib (n)
(let ( (f0 0) (f1 1) (counter 1) )
(loop
(if (>= counter n) (return-from fib f1) )
(let* ( (tmp f0) )
(setf f0 f1) (setf f1 (+ f1 tmp)) (incf counter)))))
FIB
[2]> (fib 1)
1
[3]> (fib 2)
1
[4]> (fib 3)
2
[5]> (fib 4)
3
[6]> (fib 5)
5
[7]> (fib 6)
[1]> (defun fib (n)
(let ( (f0 0) (f1 1) (counter 1) )
(loop
(if (>= counter n) (return-from fib f1) )
(let* ( (tmp f0) )
(setf f0 f1) (setf f1 (+ f1 tmp)) (incf counter)))))
FIB
[2]> (fib 1)
1
[3]> (fib 2)
1
[4]> (fib 3)
2
[5]> (fib 4)
3
[6]> (fib 5)
5
[7]> (fib 6)
8
[8]> (fib 20)
6765


Clearly, consistent with : f_1=1 f_2=1 and f_{n+1}=f_{n} + f_{n-1}, and
the value of fib(20) agrees with the tables.

Anyway, thanks.
From: J. I. Gyasu on
qikink wrote:
> On Sep 18, 9:18 pm, "J. I. Gyasu" <j.i.gyasu(a)nospam> wrote:
>> After a bit of effort, my first working lisp code which is slightly more
>> complex than printing "hello", it returns the nth fibonacci number.
>> How would you lisp gurus have written the code in the proper lisp way.
> <code>
> (defun fib (n)
> (cond
> ((= n 0) 1)
> ((= n 1) 1)
> (t (+ (fib (- n 1)) (fib (- n 2))))
> )
> )
> </code>


The above one hangs while computing (fib 200)
From: Timofei Shatrov on
On Wed, 19 Sep 2007 06:25:16 -0000, qikink <qikink(a)gmail.com> tried to confuse
everyone with this message:

>On Sep 18, 9:18 pm, "J. I. Gyasu" <j.i.gyasu(a)nospam> wrote:
>> After a bit of effort, my first working lisp code which is slightly more
>> complex than printing "hello", it returns the nth fibonacci number.
>> How would you lisp gurus have written the code in the proper lisp way.
>>
>> (defun fib (n)
>> (let ( (f0 0) (f1 1) (counter 1) )
>> (loop
>> (if (>= counter n) (return-from fib f1) )
>> (let* ( (tmp f0) )
>> (setf f0 f1) (setf f1 (+ f1 tmp)) (incf counter)))))
><code>
>(defun fib (n)
> (cond
> ((= n 0) 1)
> ((= n 1) 1)
> (t (+ (fib (- n 1)) (fib (- n 2))))
> )
>)
></code>
>That is ur basic fibonacci program.

His program is actually better than yours...

--
|Don't believe this - you're not worthless ,gr---------.ru
|It's us against millions and we can't take them all... | ue il |
|But we can take them on! | @ma |
| (A Wilhelm Scream - The Rip) |______________|
From: J. I. Gyasu on
Ken Tilton wrote:
>
>
> J. I. Gyasu wrote:
>
> Cool, your homework solution fails on the first fibonnaci number. That
> was the only one I got right.

In case you nitpick was about the count starting from 0.

[1]> (defun fib (n)
(if (= n 0) (return-from fib 0))
(let ( (f0 0) (f1 1) (counter 1) )
(loop
(if (>= counter n) (return-from fib f1) )
(let* ( (tmp f0) )
(setf f0 f1) (setf f1 (+ f1 tmp)) (incf counter)))))
FIB
[2]> (fib 0)
0
[3]> (fib 1)
1
[4]> (fib 2)
1
[5]> (fib 3)
2
[6]> (fib 4)
3
[7]> (fib 5)
5
[8]> (fib 20)
6765
From: J. I. Gyasu on
Thanks Alan. That was very informative.