...

4. Can you create a parse rule that recognizes whether a string of
numbers is from the Fibonacci sequence?
...

yes, the one I wrote will recognize any additive sequence
it could readily be extended to accept only fibs

I don't know if this would be viewed as kludgy or not 
I had wanted to use the mark: :mark without having to 
explicitly set mark: with 'find but the inner recurse rule 
did not honnor the repositioning of the parse stream in the outter rule.

that is 

rule: [ copy a foo mark: copy b foo :mark rule | end ]

did not work as I had thought/hoped/expected it would

          ... nice weather     eh          [EMAIL PROTECTED]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

REBOL[ 
    Title:   "is additive"
    Date:    10-Jan-2000
    Author:  "Tom Conlin"
    Email:   [EMAIL PROTECTED]
    File:    %isadd.r
    Purpose:{ response to question asked on the mailing list
"Can you create a parse rule that recognizes whether a 
string of numbers is from the Fibonacci sequence?"

As this is a fairly open to interpertation I make a few 
qualifing comments
    the numbers in the string are distinct from on another
    the nunbers numbers form a contigous subsequence
    the sequence is ordered small to large

the intresting part of such a parse rule has less to do with
with the Fibonacci sequence (0,1,1,2,3,5,8,13 ... ) as it has to do with 
Additive sequences which the Fibonacci sequence is just one of.
others include the Lucus sequence (1,3,4,7,11 ...) and there are 
countless others

So the goal of this parse rule is to accept Additive sequences 

if you are particular and still want only Fibonacci subsequence
take the first two numbers and repeatedly subtract one from another
till the result is zero or less; if the two numbers have become 0 & 1 
then the first two numbers of the string are a Fibonacci subsequence
if the string is also an additive sequence then the entire string
is a Fibonacci subsequence, but the first part is more math than parsing
so I'll leave it to someone else.
}  
]

seq:    copy "1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181"
seqII:  copy "1 2 3 5 8 13 21 34 buggered! 233 377 610 987 1597 2584 4181"

digit:  charset "0123456789"
digits: [some digit]

additive: [ 
    (mark: find mark b)
    :mark
    any " " copy a digits
    any " " copy b digits
    any " " copy c digits any " "
    (if ((to-integer c) - (to-integer b) - (to-integer a)) <> 0 
            [mark: back tail seq b: back tail seq]) ; -- sabotage       
    additive | end  
]

mark: seq
b: mark/1 
print parse/all  seq additive

mark: seqII
b: mark/1 
print parse/all  seqII additive

Reply via email to