Language 84

File

language84-0.7/z.84

{
:Prefix -
:Infix <
:Infix <=
:Infix >
:Infix >=
:Infix =
:Infix !=
:Infix +
:Infix -
:Infix *
:Infix /
:Infix %
:read
:show
:compare
:min
:max
:pow
:gcd
:abs
:fold
}

Where

Define (pow b n)
    Iterate {a b n} From {1 b n}
        Let a If [[n % 2] = 1] [a * b] a
        Let n [n / 2]
        In
        If [n = 0]
            a
            (Continue a [b * b] n)

Define (gcd b a)
    Iterate {b a}
        If [a = 0]
            (abs b)
            (Continue a [b % a])

Define (fold n z succ)
    Iterate {i r} From {0 z}
        If [i = n]
            r
            (Continue [i + 1] (succ i r))

Where

Define (read text)
    Let n (Prim chunk_size text)
    In
    Iterate {i num} From {0 0}
        Cond {
        | [i = n] 'just.num
        | True
            Let c (Prim chunk_fetch_uint8 text i)
            In
            If (And [c >= `0`] [c <= `9`])
                (Continue [i + 1] [[10 * num] + [c - `0`]])
                'nothing
        }

Define (show i)
    (Prim show_integer i)

Where

Define (compare a b)
    Cond {
    | [a < b] 'less
    | [a > b] 'greater
    | True 'equal
    }

Define (min a b)
    If [a < b] a b

Define (max a b)
    If [a > b] a b

Define (abs n)
    If [n < 0] -n n

Where

Define -a (Prim negate a)

Define [a < b] (Prim less a b)
Define [a <= b] (Prim less_or_equal a b)
Define [a > b] (Prim greater a b)
Define [a >= b] (Prim greater_or_equal a b)
Define [a = b] (Prim equal a b)
Define [a != b] If (Prim equal a b) False True

Define [[a] + b] (Prim add a b)
Define [[a] - b] (Prim subtract a b)
Define [[a] * b] (Prim multiply a b)
Define [a / b] (Prim quotient a b)
Define [a % b] (Prim remainder a b)