Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

unfolding fibonnaci

0 views
Skip to first unread message

Nick Keighley

unread,
Jan 21, 2010, 4:43:13 AM1/21/10
to
I've been fooling around with SRFI-1 (the list one) and I tried to
generate the fibonnaci sequence (it was a homework question in Another
Language so the odd start sequence is from the original question). I
came up with this

(define (fib n)
(unfold
(lambda (x) (= (car x) n))
(lambda (x) (cadr x))
(lambda (x) (list (+ (car x) 1) (caddr x) (+ (cadr x) (caddr
x))))
(list 0 0 1) ))

is that good? bad? It passes quite a bit of state information around.
Could it be done better? I'm not used to thinking functionally.

namekuseijin

unread,
Jan 21, 2010, 11:15:36 AM1/21/10
to
Nick Keighley escreveu:

> I've been fooling around with SRFI-1 (the list one) and I tried to
> generate the fibonnaci sequence (it was a homework question in Another
> Language so the odd start sequence is from the original question). I
> came up with this
>
> (define (fib n)
> (unfold
> (lambda (x) (= (car x) n))
> (lambda (x) (cadr x))
> (lambda (x) (list (+ (car x) 1) (caddr x) (+ (cadr x) (caddr
> x))))
> (list 0 0 1) ))
>
> is that good? bad?

why construct lists?

(define (fib n)
(let loop ((n n) (fibo 1) (fibo0 1))
(if (< n 2) fibo
(loop (- n 1) (+ fibo fibo0) fibo))))

--
a game sig: http://tinyurl.com/d3rxz9

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Nick Keighley

unread,
Jan 22, 2010, 5:03:12 AM1/22/10
to
On 21 Jan, 16:15, namekuseijin <namekusei...@gmail.com> wrote:
> Nick Keighley escreveu:
>
> > I've been fooling around with SRFI-1 (the list one) and I tried to
> > generate the fibonnaci sequence (it was a homework question in Another
> > Language so the odd start sequence is from the original question). I
> > came up with this
>
> > (define (fib n)
> >     (unfold
> >         (lambda (x) (= (car x) n))
> >         (lambda (x) (cadr x))
> >         (lambda (x) (list (+ (car x) 1) (caddr x) (+ (cadr x) (caddr
> > x))))
> >         (list 0 0 1) ))
>
> > is that good? bad?
>
> why construct lists?
>
> (define (fib n)
>    (let loop ((n n) (fibo 1) (fibo0 1))
>      (if (< n 2) fibo
>          (loop (- n 1) (+ fibo fibo0) fibo))))

because I was trying to use unfold.


0 new messages