>
> On Fri, Jan 11, 2013 at 9:47 AM, John Benediktsson <mrj...@gmail.com>wrote:
>
>> This should be a direct translation of that method, I think
>>
>> :: fibonacci? ( n )
>> 1 [ dup fibonacci n < ] [ 1 + ] while fibonacci n = ;
>>
>
Factored this to have only one reference to a local.
:: closest-upper-index ( n -- m )
1 [ dup fibonacci n < ] [ 1 + ] while ;
: closest-upper ( n -- m )
closest-upper-index fibonacci ;
: fibonacci? ( n -- ? )
dup closest-upper = ;
Then wondered if there is a way to factor out the remaining local.
Wondering if there is a way to generalize a for loop, using [|.
Here's the original Algol code ...
boolean fibonacci?( int n ) {
int i = 1;
while( fibonacci( i ) < n ) {
i++;
}
return fibonacci( i ) == n;
}
... which is the same as ...
boolean fibonacci?( int n ) {
for( int i = 1; fibonacci( i ) < n; i++ ) {
}
return fibonacci( i ) == n;
}
Wondering if the for loop can be generalized, kind of like how cond-case
was constructed in the fizzbuzz program ...
http://re-factor.blogspot.com/2011/08/fizzbuzz.html
... but on a more massive scale.
Imagine if it is possible to mimic every Algol construct in Factor with
objects, quotations, and fry specifiers.
It would essentially make most languages/compilers obsolete, because one
could "think" in Algol, in Factor.
Not at expert, so not sure how to go about it.
Would start by defining a counter object that stores an integer counter
variable, i; a constant, n; a boolean test variable, test; and some
quotations.
"for" is a method on the counter object.
It might do the following, in Algol ...
for( "i already initialized" ; quot1; quot2 ) {
quot3;
}
quot1 evaluates and stores in test on each iteration.
quot2 evaluates and stores in i on each iteration.
quot3 evaluates and gets left on the stack on each iteration.
There should be a mechanism for frying all quotes with i and n.
Not sure how to do that, but it seems possible.
Is there a way to bind a fry specifier to an instance variable?
:: closest-upper-index ( n -- m )
1 n [ i fibonacci n < ] [ 1 + ] [] <counter>
for ;
In this case, the n in [ i fibonacci n < ] binds to counter.n, not the
word's local n.
Still, counter.n is initialized to the word's local n.
The question is whether it is possible.
Not sure if it is already accomplished with "mutable bindings".
http://docs.factorcode.org/content/article-locals-examples.html
It seems that the <counter> example leaves its counted values on the stack.
Proposed here is a <counter> object that stores counting internally.
It stores the result of quot1 and quot2 in instance variables on each
iteration, before dropping them off the stack.
Only the result of quot3 gets left on the stack.
Maybe there could be a generic iterator pattern that stores a hashmap of
variables and quotes.
The result of evaluating a quote gets stored in the corresponding key
variable.
There is one main quote, that is the body of the iteration.
This would cover loops with more inputs than a counter and a constant.
It seems like a good idea at 4am, but maybe it's just reinventing the C
compiler?
Still, somehow the idea that Algol could be implemented as a
domain-specific-language in Factor seems philosophical.
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk