[stack] 'while' derived from 's', 'k', and 'if' in Factor

John Nowak <[email protected]>
Newsgroups gmane.comp.lang.concatenative
Message-ID <[email protected]>
I took the 's' and 'k' combinators from here:
http://tunes.org/~iepos/joy.html

Yes, the definition for 's' could be much better, but I don't know  
really know Factor's combinators. For the Joy programmers, note that  
Factor's 'if' requires a boolean on the stack, not a quotation that  
yields a boolean.

Ah well. Back to work.

- John

- - - - - - - - - -

! The 's' combinator
! [$A] [$B] [$C] [$D] s == [[$A] $B] $D [$A] $C
: s
   [ [ over ] dip ] dip
   [ [ [ curry ] dip ] dip ] dip
   -rot
   [ [ call ] dip ] dip
   call
;

! The 'k' combinator
! [$A] [$B] k == $B
: k nip call ;

! Essential combinators:
: j [ k ] [ [ ] [ [ ] ] ] [ ] s s s ;
: qc [ ] [ ] [ ] s ;
: d [ ] k ;
: q qc d ;
: x q j ;
: i [ ] x j d ;
: c qc x i ;
: p [ ] [ ] s d ;
: m c i ;
: o [ x j i ] p p ;
: v x c [ x ] j ;
: c2 v v ;

! The 'w' ("while") combinator
! [$A] [$B] w == $B [$A [$A] [$B] w] [ ] if
! Example usage: 10 [ c . 1 - ] [ c 0 > ] w d
: w [ [ c2 ] j c p p p [ x ] j o x j [ ] if ] m ;

! Key:
! j  = dip
! qc = dup quote swap
! d  = drop
! q  = quote
! x  = swap
! i  = call (factor), apply (cat), i (joy)
! c  = copy, dup
! p  = partial-apply, curry (factor, cat), cons (joy)
! m  = mockingbird, dup i
! o  = compose
! v  = over
! c2 = 2dup
! w  = while (A [A -> A] [A -> A Bool] -> A)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.