> Then wondered if there is a way to factor out the remaining local.

Could just use fried quotations:

  : closest-upper-index ( n -- m )
      1 swap '[ dup fibonacci _ < ] [ 1 + ] while ;

> Imagine if it is possible to mimic every Algol construct in Factor with
> objects, quotations, and fry specifiers.

Strictly speaking, it already is possible...which isn't saying much:
https://en.wikipedia.org/wiki/Turing_completeness

> It would essentially make most languages/compilers obsolete, because one
> could "think" in Algol, in Factor.

As much as any one language makes another "obsolete", I suppose.

> "for" is a method on the counter object.

If `for` wouldn't be used on another type of object, it could just as well
be a
normal word.

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

Seems like a lot of work to reinvent the `while` loop.  There's already
something to store "variables": the stack.  Also, it's explicit what you
want
to stay on the stack and what you want dropped, because you call some
variant
of `drop`.  For instance:

  $ grep -r "while [234]\?drop" *
  basis/json/reader/reader.factor:    '[ _ stream-read1 dup ] [ scan ]
while drop ;
  basis/io/encodings/string/string.factor:        [ reader stream-read1 dup
] [ buf push ] while drop
  basis/math/primes/factors/factors.factor:    [ dupd /mod zero? ] curry [
nip [ 1 + ] dip ] while drop
  basis/random/sfmt/sfmt.factor:    ] while drop ;
  basis/random/random.factor:    ] while drop ;
  basis/random/random.factor:    while drop ;
  basis/random/random.factor:    [ [ 1 + ] 2dip [ random-unit log neg + ]
dip ] while 2drop ;
  basis/csv/csv.factor:    [ row ] do while drop ;
  core/sorting/sorting.factor:    while 2drop ; inline
  core/io/io.factor:    [ dup ] compose swap while drop ; inline
  extra/pdf/layout/layout.factor:    ] while drop ;
  extra/images/atlas/atlas.factor:    [ image-placements atlas-width @y
(pack-stripe) dup ] [ @y + @y! ] while drop
  extra/rosetta-code/happy-numbers/happy-numbers.factor:        ] while
2drop
  extra/rosetta-code/tree-traversal/tree-traversal.factor:    ] while drop
; inline
  extra/rosetta-code/luhn-test/luhn-test.factor:    while drop ;
  extra/audio/wav/wav.factor:    } cond ] while drop
  extra/audio/aiff/aiff.factor:    } cond ] while drop
  extra/io/files/trash/unix/unix.factor:    ] while drop nip ;
  extra/tokyo/assoc-functor/assoc-functor.factor:    ] while 3drop ;
  extra/libudev/libudev.factor:    while drop ; inline
  extra/reddit/reddit.factor:        [ next-page ] while drop
  extra/project-euler/112/112.factor:    ] do while 2drop ;
  unmaintained/graph-theory/graph-theory.factor:       while 2drop ] swap
search-wrap ; inline
  unmaintained/adsoda/adsoda.factor:    [ [ test-faces-combinaisons ] 2keep
1 - ] while drop ;

The reason I note `while` in particular being that `for` loops are really
just
variants of `while` in most languages:

  IN: scratchpad USE: combinators.smart
  IN: scratchpad : smart-bi ( p q -- ) [ keep-inputs ] dip call ; inline
  IN: scratchpad : for ( test update body -- ) swap [ smart-bi ] 2curry
while ; inline
  IN: scratchpad 1 [ dup 10 < ] [ 1 + ] [ . ] for drop
  1
  2
  3
  4
  5
  6
  7
  8
  9
  IN: scratchpad 1 10 [ 2dup < ] [ [ 1 + ] [ 1 - ] bi* ] [ 2array . ] for
2drop
  { 1 10 }
  { 2 9 }
  { 3 8 }
  { 4 7 }
  { 5 6 }

Though maybe there's a way to use smart combinators to get rid of the last
iteration's "update" return values, so you don't have to drop them
explicitly.
Or, don't use smart combinators at all, and make explicit words like `for`,
`2for`, etc.

Regards,
--Alex Vondrak


On Sat, Jan 12, 2013 at 3:12 AM, Leonard P <leonard14...@gmail.com> wrote:

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

Reply via email to