Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Fibonacci series
Pages: [1] Print
Author Topic: Fibonacci series  (Read 680 times)
leke
Jr. Member
**
Offline Offline

Posts: 92


View Profile WWW
Fibonacci series
« on: September 15, 2004, 04:13:35 AM »


I have been learning the WHILE loop in REBOL. The only examples in the Core Guide seem to use it in series like this:

    colors: [red green blue yellow orange]

    while [not tail? colors] [
        print first colors
        colors: next colors
    ]

    red
    green
    blue
    yellow
    orange

Could any one give me a REBOL version of this peice of code:
 
>>> a, b = 0, 1
>>> while b < 10:
...       print b
...       a, b = b, a+b

I tried:

REBOL []
a: 0
b: 1
while [b < 100]
[print b
   a: b      
   b: a + b]

halt

but this is wrong somehow.
It's meant to print out the Fibonacci series
 
Logged

DideC
Newbie
*
Offline Offline

Posts: 32


View Profile
Fibonacci series
« Reply #1 on: September 15, 2004, 03:19:18 PM »

The problem is on 'a

In the pseudo code you try to implement, you affect 2 values to two vars in one line :
Quote
... a, b = b, a+b
But the Rebol code you post doesn't do that :
Quote
a: b
b: a + b]
As the 'b value is affected to 'a, you have a = b and the last line mean b: b + b

You can solve the problem by picking the 'a value and affecting the 'b value to it just after, all this in the 'b computation :
Code:
Rebol []
a: 0
b: 1
while [b < 100] [
print b
b: a + a: b
]
Logged
RebolBert
Guest


Email
Re: Fibonacci series
« Reply #2 on: May 08, 2006, 09:07:31 AM »

Quote
Could any one give me a REBOL version of this peice of code:
 
>>> a, b = 0, 1
>>> while b < 10:
...       print b
...       a, b = b, a+b

Yes :

Code:
set [a b] [0 1]
while [b < 10] [
    print b
    set [a b] reduce [b  a + b]
]

Logged
Pages: [1] Print 
Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Fibonacci series
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 20, 2008, 05:41:00 AM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2285 Posts in 594 Topics by 3708 Members
Latest Member: cialissactiv

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc