# 'while' derived from 's', 'k', and 'if' in Factor

## John Nowak — 2008-05-18 01:39:23

    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)