Language 84

File

language84-0.5/odds_evens.84

\   Solving Alan Kay's odds-evens problem without mutual recursion. See "Early
\   History of Smalltalk".

Block
    Define (run f)
        (LIST.for_each
            (f [Right "one" & "two" & "three" & "four" & "five" & 'nil])
            STDIO.print_line)
    In
    Begin
        (run odds_evens1)
        (STDIO.print_line "")
        (run odds_evens2)
    End

Where

Define (odds_evens1 items)
    Define (select mode items)
        Unfold {mode items}
            Match items
            | 'nil 'nil
            | 'cons.{item items}
                Match mode
                | 'odds [item & (Fold 'evens items)]
                | 'evens (Fold 'odds items)
                ;
            ;
    In
    (LIST.append (select 'odds items) (select 'evens items))

Define (odds_evens2 items)
    Let {odds evens}
        Define (o item {odds evens}) {[item & odds] evens}
        Define (e item {odds evens}) {odds [item & evens]}
        In
        Unfold {here there items} From {o e items}
            Match items
            | 'nil {'nil 'nil}
            | 'cons.{item items} (here item (Fold there here items))
            ;
    In
    (LIST.append odds evens)

Where

Let LIST Package "list"
Let STDIO Package "stdio"