Language 84

File

language84-0.4/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)
        Define (rec rec mode items)
            Match items
            | 'nil 'nil
            | 'cons.{item items}
                Match mode
                | 'odds [item & (rec rec 'evens items)]
                | 'evens (rec rec 'odds items)
                ;
            ;
        In
        (rec rec mode items)
    In
    (LIST.append (select 'odds items) (select 'evens items))

Define (odds_evens2 items)
    Let {odds evens}
        For (rec rec
                Func {item {odds evens}} {[item & odds] evens}
                Func {item {odds evens}} {odds [item & evens]}
                items)
            Define (rec rec here there items)
                Match items
                | 'nil {'nil 'nil}
                | 'cons.{item items} (here item (rec rec there here items))
                ;
    In
    (LIST.append odds evens)

Where

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