Language 84

File

language84-0.4/list.84

{
: cons
: fold
: reduce
: for_each
: for_each_numbered
: iota
: map
: filter
: length
: reverse
: reverse_append
: append
: concat
: concat_map
: fetch
: zip
: unzip
: last
: join
: repeat
}

Where

Define (cons head tail)
    [head & tail]

Define (map list transform)
    (fold list 'nil Func {elem list} [(transform elem) & list])

Define (iota n)
    Define (rec rec i)
        If [i = n]
            'nil
            [i & (rec rec [i + 1])]
    In
    (rec rec 0)

Define (filter list pred)
    (fold list 'nil Func {elem list} If (pred elem) [elem & list] list)

Define (length list)
    (reduce list 0
        Func {count _} [count + 1])

Define (concat lists)
    (fold lists 'nil append)

Define (concat_map list f)
    (fold list 'nil
        Func {item items} (append (f item) items))

Define (fetch list n)
    If [n < 0]
        'nothing
        Iterate {list n}
            Match list
            | 'cons.{item list}
                If [n = 0]
                    'just.item
                    Continue {list [n - 1]}
            | 'nil 'nothing
            ;

Define (zip a b)
    Iterate {a b z} From {a b 'nil}
        Match a
        | 'cons.{ai a}
            Match b
            | 'cons.{bi b} Continue {a b [{ai bi} & z]}
            | 'nil (reverse z)
            ;
        | 'nil (reverse z)
        ;

Define (unzip list)
    (fold list {'nil 'nil}
        Func {{a b} {as bs}} {[a & as] [b & bs]})

Define (last items)
    (reduce items 'nothing Func {_ item} 'just.item)

Define (join glue lists)
    (fold lists 'nil
        Func {list lists}
            Match lists
            | 'nil [list & 'nil]
            | 'cons._ [Right list & glue & lists]
            ;)

Define (repeat n elem)
    Define (rec rec n)
        If [n = 0] 'nil [elem & (rec rec [n - 1])]
    In
    (rec rec n)

Where

Define (append left right) (reverse_append (reverse left) right)

Where

Define (reverse list) (reverse_append list 'nil)

Where

Define (reverse_append queue tail)
    (reduce queue tail
        Func {list elem} [elem & list])

Where

Define (fold list nil cons)
    Define (rec rec list)
        Match list
        | 'nil nil
        | 'cons.{elem list} (cons elem (rec rec list))
        ;
    In
    (rec rec list)

Define (reduce list zero plus)
    Iterate {list sum} From {list zero}
        Match list
        | 'nil sum
        | 'cons.{elem list} Continue {list (plus sum elem)}
        ;

Define (for_each list command)
    Iterate {list}
        Match list
        | 'nil {}
        | 'cons.{elem list}
            Begin
                (command elem)
                Continue {list}
            End
        ;

Define (for_each_numbered list command)
    Iterate {i list} From {0 list}
        Match list
        | 'nil {}
        | 'cons.{elem list}
            Begin
                (command i elem)
                Continue {[i + 1] list}
            End
        ;