From: J. I. Gyasu
Subject: beggining lisp
Date: 
Message-ID: <46f0a340$0$7450$4c368faf@roadrunner.com>
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)))))

From: D Herring
Subject: Re: beggining lisp
Date: 
Message-ID: <R5idnWnFjOWGLG3bnZ2dnUVZ_vihnZ2d@comcast.com>
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.
> 
> (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)))))

Do any of these follow the proper lisp way?
Probably not -- I'm not a guru.  ;)

- Daniel

(defun f (n)
   (prog1
     (round
      (/ (- (expt (/ (+ 1 (sqrt 5)) 2) n)
            (expt (/ (- 1 (sqrt 5)) 2) n))
         (sqrt 5)))))

(defun g (n)
   (if (> n 2)
       (+ (g (- n 1))
          (g (- n 2)))
       1))

(defun h (n)
   (loop for x from 1 upto n
         for a = 0 then b
         and b = 1 then (+ a b)
         finally (return b)))

(defun i (n)
   (labels ((iter (n &optional (a 0) (b 1))
              (if (<= n 1)
                  b
                  (iter (1- n) b (+ a b)))))
     (iter n)))


(let ((*table* nil)
       (*step* 10))
   (defun j (n)
     "Overcomplicated memoization"
     (cond ((eql n :table)
            (return-from j *table*))
           ((eql n :step)
            (return-from j *step*))
           ((<= n 0)
            (return-from j 0))
           ((= n 1)
            (return-from j 1)))
     (do ((memos (* (length *table*) *step*)
                 (* (length *table*) *step*)))
         ((< (- n 1) memos))
       (let* ((last (car (last *table*)))
              (new (make-array (list *step*)))
              (a (if last
                     (svref last (- *step* 2))
                     0))
              (b (if last
                     (svref last (- *step* 1))
                     1)))
         (dotimes (k *step*)
           (setf (svref new k) (+ a b)
                 a b
                 b (svref new k)))
         (setf *table* (append *table* (list new)))))
     (multiple-value-bind (q r) (floor (- n 2) *step*)
       (svref (nth q *table*) r))))
From: Giorgos Keramidas
Subject: Re: beggining lisp
Date: 
Message-ID: <87ps09kkkc.fsf@kobe.laptop>
On Wed, 19 Sep 2007 01:22:22 -0400,
D Herring <········@at.tentpost.dot.com> 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.
>>
>> (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)))))

Hi J.I.,

IMHO there are too many LET forms and RETURN-FROM seems a bit "Un-Lispy"
indeed, but taste is a very personal thing, so if you like things this
way, then who are we to judge your code by our personal definition of
what is "tasteful" or "Lispy"? :-)

> Do any of these follow the proper lisp way?
> Probably not -- I'm not a guru.  ;)

Nice collection you have there!

> (defun f (n) ...)
> (defun g (n) ...)
> (defun h (n)
>   (loop for x from 1 upto n
>         for a = 0 then b
>         and b = 1 then (+ a b)
>         finally (return b)))

Since [1..n] has the same size as [0..(n-1)] this version looks very
cool already, and "from 1 upto n" can be replaced with "below n":

    CL-USER> (defun fib (n)
               (loop for x below n
                  for a = 0 then b
                  and b = 1 then (+ a b)
                  finally (return b)))
    CL-USER> (mapcar #'fib (loop for x from 1 upto 20 collecting x))
    (1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765)
    CL-USER>

One version which I didn't see listed is the one using DO, which has the
interesting property of numbering Fibonacci's starting from a 0th number
instead of a 1st:

    CL-USER> (defun fib (num)
               (do ((a 0 b)
                    (b 1 (+ a b))
                    (x 0 (incf x)))
                   ((= x num) b)
                 nil))
    CL-USER> (mapcar #'fib (loop for x below 20 collecting x))
    (1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765)
    CL-USER>

Then after staring at this for a while and re-ordering only a wee bit
the LOOP-based version it is trivial to also write a LOOP-based version
supporting a 0th Fibonnaci:

    CL-USER> (defun fib (num)
               (loop for a = 0 then b
                  and b = 1 then (+ a b)
                  for x below num
                  finally (return b)))
    CL-USER> (mapcar #'fib (loop for x below 20 collecting x))
    (1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765)
    CL-USER>

In general, I tend to avoid DO if I can help it, but in this case it
helped me understand more about the way LOOP works, so _thanks_ for
triggerring this with your initial collection :)

- Giorgos
From: Nicolas Neuss
Subject: Re: beggining lisp
Date: 
Message-ID: <871wcogsu6.fsf@ma-patru.mathematik.uni-karlsruhe.de>
Giorgos Keramidas <········@ceid.upatras.gr> writes:

>     CL-USER> (defun fib (n)
>                (loop for x below n
>                   for a = 0 then b
>                   and b = 1 then (+ a b)
>                   finally (return b)))
>     CL-USER> (mapcar #'fib (loop for x from 1 upto 20 collecting x))
>     (1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765)
>     CL-USER>

0-th Fibonacci is 0, IIRC.  Furthermore, the use of loop's repeat clause is
appropriate here.  Thus,

(defun fib (n)
   (loop repeat n
         for a = 0 then b
         and b = 1 then (+ a b)
         finally (return a)))

Nicolas
From: Ken Tilton
Subject: Re: beggining lisp
Date: 
Message-ID: <pB2Ii.363$Sf2.172@newsfe12.lga>
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?

> 
> (defun fib (n)
>   (let ( (f0 0) (f1 1) (counter 1) )

loop can do let.

>     (loop
>      (if (>= counter n) (return-from fib f1) )

else?

>      (let* ( (tmp f0) )

Where are the dependent clauses of this let*?

>        (setf f0 f1) (setf f1 (+ f1 tmp)) (incf counter)))))

Loop can step.

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

kt

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut
From: ············@gmail.com
Subject: Re: beggining lisp
Date: 
Message-ID: <1190212872.114317.110610@g4g2000hsf.googlegroups.com>
>
> Cool, your homework solution fails on the first fibonnaci number. That
> was the only one I got right.
>
> kt
>
> --http://www.theoryyalgebra.com/
>
> "We are what we pretend to be." -Kurt Vonnegut


Interestingly, the ratio of two successive elements in the fib
sequence converges to the golden ratio.  This is true for any sequence
that starts with non-negative and positive first two elements and
follows the fib rule a_n = a_{n-1} + a_{n-2}.    Like {2 5 7 12 ...}.
Or {1 100 101 ...}.  So, hey, who cares about a little off-by-one
error!
From: J. I. Gyasu
Subject: Re: beggining lisp
Date: 
Message-ID: <46f116ef$0$32558$4c368faf@roadrunner.com>
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: John Thingstad
Subject: Re: beggining lisp
Date: 
Message-ID: <op.tywj7wgqpqzri1@pandora.upc.no>
P� Wed, 19 Sep 2007 14:32:15 +0200, skrev J. I. Gyasu <·········@nospam>:

> 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?
>>

Kenny! Shouldnt you be writing that algebra program you have been  
promising for several years.. What are you doing here harassing newbies  
and playing Erik Naggum. Get a grip!
From: J. I. Gyasu
Subject: Re: beggining lisp
Date: 
Message-ID: <46F11BB9.4050703@nospam>
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: Ken Tilton
Subject: Re: beggining lisp
Date: 
Message-ID: <Cr9Ii.11$9b2.8@newsfe12.lga>
J. I. Gyasu wrote:
> 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....

You Americans are all alike, and may I just express here my undying 
admiration for your obsession with software correctness allowing you to 
classify "does not work" as a nit. There is a great future for you in 
software. Yes, we have a pool. We have a pool and a pond. The pond would 
be good for you.*

Remember when we were all hoping Lisp would become more popular and I 
said be careful what you wish for? Did anyone listen to me? Does anyone 
ever listen to me?

hth, kenny

* Ty to Carl in Caddy Shack

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut
From: Thomas F. Burdick
Subject: Re: beggining lisp
Date: 
Message-ID: <1190214444.462938.156470@z24g2000prh.googlegroups.com>
On Sep 19, 3:23 pm, Ken Tilton <···········@optonline.net> wrote:
> J. I. Gyasu wrote:
> > 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....
>
> You Americans are all alike, and may I just express here my undying
> admiration for your obsession with software correctness allowing you to
> classify "does not work" as a nit. There is a great future for you in
> software. Yes, we have a pool. We have a pool and a pond. The pond would
> be good for you.*
>
> Remember when we were all hoping Lisp would become more popular and I
> said be careful what you wish for? Did anyone listen to me? Does anyone
> ever listen to me?

Kenny, if you're going to sneak out of the Cabal of Lispers meetings
to check on the game at the sports bar, at least ask people what you
missed before complaining in public, will ya?  We need a sufficient
number of debutants actively engaged in the language if we want to
make the Semantic Winter really stick.  This little wave now is a
small price to pay for the insulation they'll buy us later.  And
remember your talking points: *Lisp* will transform the web into one
giant AI.  If we're not careful, Python or Ruby might take credit for
the failure and then we're screwed.
From: Ken Tilton
Subject: Re: beggining lisp
Date: 
Message-ID: <YSbIi.37$Q66.8@newsfe12.lga>
Thomas F. Burdick wrote:
> On Sep 19, 3:23 pm, Ken Tilton <···········@optonline.net> wrote:
> 
>>J. I. Gyasu wrote:
>>
>>>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....
>>
>>You Americans are all alike, and may I just express here my undying
>>admiration for your obsession with software correctness allowing you to
>>classify "does not work" as a nit. There is a great future for you in
>>software. Yes, we have a pool. We have a pool and a pond. The pond would
>>be good for you.*
>>
>>Remember when we were all hoping Lisp would become more popular and I
>>said be careful what you wish for? Did anyone listen to me? Does anyone
>>ever listen to me?
> 
> 
> Kenny, if you're going to sneak out of the Cabal of Lispers meetings
> to check on the game at the sports bar, at least ask people what you
> missed before complaining in public, will ya?  We need a sufficient
> number of debutants actively engaged in the language if we want to
> make the Semantic Winter really stick.  This little wave now is a
> small price to pay for the insulation they'll buy us later.  And
> remember your talking points: *Lisp* will transform the web into one
> giant AI.  If we're not careful, Python or Ruby might take credit for
> the failure and then we're screwed.
> 

I think we'll be OK:

All sufficiently ambitious software projects fail.
Lisp is used for sufficiently ambitious software projects.
Therefore all software projects fail because of Lisp.

kenny

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut
From: Slobodan Blazeski
Subject: Re: beggining lisp
Date: 
Message-ID: <1190635301.189714.89730@19g2000hsx.googlegroups.com>
On Sep 19, 6:09 pm, Ken Tilton <···········@optonline.net> wrote:
> Thomas F. Burdick wrote:
> > On Sep 19, 3:23 pm, Ken Tilton <···········@optonline.net> wrote:
>
> >>J. I. Gyasu wrote:
>
> >>>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....
>
> >>You Americans are all alike, and may I just express here my undying
> >>admiration for your obsession with software correctness allowing you to
> >>classify "does not work" as a nit. There is a great future for you in
> >>software. Yes, we have a pool. We have a pool and a pond. The pond would
> >>be good for you.*
>
> >>Remember when we were all hoping Lisp would become more popular and I
> >>said be careful what you wish for? Did anyone listen to me? Does anyone
> >>ever listen to me?
>
> > Kenny, if you're going to sneak out of the Cabal of Lispers meetings
> > to check on the game at the sports bar, at least ask people what you
> > missed before complaining in public, will ya?  We need a sufficient
> > number of debutants actively engaged in the language if we want to
> > make the Semantic Winter really stick.  This little wave now is a
> > small price to pay for the insulation they'll buy us later.  And
> > remember your talking points: *Lisp* will transform the web into one
> > giant AI.  If we're not careful, Python or Ruby might take credit for
> > the failure and then we're screwed.
>
> I think we'll be OK:
>
> All sufficiently ambitious software projects fail.
> Lisp is used for sufficiently ambitious software projects.
> Therefore all software projects fail because of Lisp.
>
> kenny
>
> --http://www.theoryyalgebra.com/

What do you suggest ? Use lisp only for toy projects and blame other
language for failures?
From: Ken Tilton
Subject: Re: beggining lisp
Date: 
Message-ID: <gPOJi.14$B01.3@newsfe12.lga>
Slobodan Blazeski wrote:
> On Sep 19, 6:09 pm, Ken Tilton <···········@optonline.net> wrote:
> 
>>Thomas F. Burdick wrote:
>>
>>>On Sep 19, 3:23 pm, Ken Tilton <···········@optonline.net> wrote:
>>
>>>>J. I. Gyasu wrote:
>>
>>>>>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....
>>
>>>>You Americans are all alike, and may I just express here my undying
>>>>admiration for your obsession with software correctness allowing you to
>>>>classify "does not work" as a nit. There is a great future for you in
>>>>software. Yes, we have a pool. We have a pool and a pond. The pond would
>>>>be good for you.*
>>
>>>>Remember when we were all hoping Lisp would become more popular and I
>>>>said be careful what you wish for? Did anyone listen to me? Does anyone
>>>>ever listen to me?
>>
>>>Kenny, if you're going to sneak out of the Cabal of Lispers meetings
>>>to check on the game at the sports bar, at least ask people what you
>>>missed before complaining in public, will ya?  We need a sufficient
>>>number of debutants actively engaged in the language if we want to
>>>make the Semantic Winter really stick.  This little wave now is a
>>>small price to pay for the insulation they'll buy us later.  And
>>>remember your talking points: *Lisp* will transform the web into one
>>>giant AI.  If we're not careful, Python or Ruby might take credit for
>>>the failure and then we're screwed.
>>
>>I think we'll be OK:
>>
>>All sufficiently ambitious software projects fail.
>>Lisp is used for sufficiently ambitious software projects.
>>Therefore all software projects fail because of Lisp.
>>
>>kenny
>>
>>--http://www.theoryyalgebra.com/
> 
> 
> What do you suggest ?

I suggest no further discussion of Lisp's image problem now that it is 
the fastest growing language on Earth and will soon push all the others 
into the sea.

> Use lisp only for toy projects and blame other
> language for failures?

I suggest we shut down c.l.l and everyone get to work writing computer 
applications. Remember those? The reason we program? Hello? Anybody?

kenny

-- 
http://www.theoryyalgebra.com/

"We are what we pretend to be." -Kurt Vonnegut
From: Thomas A. Russ
Subject: Re: beggining lisp
Date: 
Message-ID: <ymilkb2765g.fsf@blackcat.isi.edu>
"J. I. Gyasu" <·········@nospam> writes:
> 
> 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)))))

A slightly more compact version, that uses a few more of Lisp's built-in
functions:

(defun fib (n)
   (if (zerop n)
        0
        (let ((f0 0) (f1 1))
          (dotimes (i (1- n))
            (psetq f0 f1 
                   f1 (+ f1 f0)))
           f1)))


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Giorgos Keramidas
Subject: Re: beggining lisp
Date: 
Message-ID: <87ejgolxmh.fsf@kobe.laptop>
On 19 Sep 2007 13:27:07 -0700, ···@sevak.isi.edu (Thomas A. Russ) wrote:
> A slightly more compact version, that uses a few more of Lisp's
> built-in functions:
>
> (defun fib (n)
>    (if (zerop n)
>         0
>         (let ((f0 0) (f1 1))
>           (dotimes (i (1- n))
>             (psetq f0 f1
>                    f1 (+ f1 f0)))
>            f1)))

Nice, but somehow it doesn't feel right when it yields:

    CL-USER> (fib -10)
    1
    CL-USER>

Tweaking it a bit by replacing (zerop n) with (< n 1), may be a good
idea, but that's nit-picking now :)
From: qikink
Subject: Re: beggining lisp
Date: 
Message-ID: <1190183116.954312.82750@e9g2000prf.googlegroups.com>
On Sep 18, 9:18 pm, "J. I. Gyasu" <·········@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. If you want what might be a
slightly nicer textobbk, or just something to help with this, check
out "Common Lisp A gentle introduction to symbolic computation" (for
free!)
at: http://www.cs.cmu.edu/~dst/LispBook/index.html
From: J. I. Gyasu
Subject: Re: beggining lisp
Date: 
Message-ID: <46f11863$0$11061$4c368faf@roadrunner.com>
qikink wrote:
> On Sep 18, 9:18 pm, "J. I. Gyasu" <·········@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: smallpond
Subject: Re: beggining lisp
Date: 
Message-ID: <1190227879.071272.85710@r29g2000hsg.googlegroups.com>
On Sep 19, 8:38 am, "J. I. Gyasu" <·········@nospam> wrote:
> qikink wrote:
> > On Sep 18, 9:18 pm, "J. I. Gyasu" <·········@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)


It may be hung or it may be just pensive.  Perhaps you
could write a short program that tells whether it will
eventually complete.
-- S
From: Slobodan Blazeski
Subject: Re: beggining lisp
Date: 
Message-ID: <1190635812.839470.323220@n39g2000hsh.googlegroups.com>
On Sep 19, 8:51 pm, smallpond <·········@juno.com> wrote:
> On Sep 19, 8:38 am, "J. I. Gyasu" <·········@nospam> wrote:
>
>
>
>
>
> > qikink wrote:
> > > On Sep 18, 9:18 pm, "J. I. Gyasu" <·········@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)
>
> It may be hung or it may be just pensive.  Perhaps you
> could write a short program that tells whether it will
> eventually complete.

Are you referring to Halting problem ?
http://www.everything2.com/index.pl?node_id=450977

Slobodan
From: Giorgos Keramidas
Subject: Re: beggining lisp
Date: 
Message-ID: <87lkaxkkaz.fsf@kobe.laptop>
On Wed, 19 Sep 2007 08:38:34 -0400, "J. I. Gyasu" <·········@nospam> wrote:
> qikink wrote:
>> On Sep 18, 9:18 pm, "J. I. Gyasu" <·········@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)

Try tracing it and watch the recursions unfold in their magnificent
glory :)
From: Timofei Shatrov
Subject: Re: beggining lisp
Date: 
Message-ID: <46f11918.187080137@news.readfreenews.net>
On Wed, 19 Sep 2007 06:25:16 -0000, qikink <······@gmail.com> tried to confuse
everyone with this message:

>On Sep 18, 9:18 pm, "J. I. Gyasu" <·········@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: Alan Crowe
Subject: Re: beggining lisp
Date: 
Message-ID: <86vea6j1nt.fsf@cawtech.freeserve.co.uk>
"J. I. Gyasu" <·········@nospam> writes:

> 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)))))

SETF takes any even number of arguments, not just two. So
you can write


(let ((tmp f0))
  (setf f0 f1
        f1 (+ f1 tmp)))

This idiom, of saving data in a temporary variable before
you overwrite it, because you need to use the old value is
supported directly by PSETF

(psetf f0 f1
       f1 (+ f1 f0))

and adding another layer, the idiom of setting up some
variables and then repeatedly updating them in parallel

(let ((f0 0)
      (f1 1)
      (counter 1))
  (loop (when (test counter) (return f1))
        (psetf f0 f1
               f1 (+ f1 f0)
               count (+ count 1))))

is supported directly by DO

CL-USER> (dotimes (i 10)
           (print (do ((current 1 (+ current previous))
                       (previous 0 current)
                       (count i (- count 1)))
                      ((zerop count) current))))

1 
1 
2 
3 
5 
8 
13 
21 
34 
55 

Alan Crowe
Edinburgh
Scotland
From: J. I. Gyasu
Subject: Re: beggining lisp
Date: 
Message-ID: <46f11efb$0$9537$4c368faf@roadrunner.com>
Thanks Alan. That was very informative.