[ create a new paste ] login | about

Link: http://codepad.org/BfuzCK5B    [ raw code | output | fork ]

Python, pasted on Nov 9:
Zeroes = lambda: [0, Zeroes]
Ones = lambda: [1, Ones]

Push = lambda b, x: \
    x if b == 0 and x == Zeroes or b == 1 and x == Ones \
    else lambda: [b, x]

Head = lambda x: x()[0]
Tail = lambda x: x()[1]

Inc = lambda x: \
    Zeroes if x == Ones \
    else Push(1, Zeroes) if x == Zeroes \
    else Push(1, Tail(x)) if Head(x) == 0 \
    else Push(0, Inc(Tail(x)))

Dec = lambda x: \
    Ones if x == Zeroes \
    else Push(0, Ones) if x == Ones \
    else Push(0, Tail(x)) if Head(x) == 1 \
    else Push(1, Dec(Tail(x)))

Plus = lambda x, y: \
       x if y == Zeroes \
       else Dec(x) if y == Ones \
       else Push(Head(y), Plus(Tail(x), Tail(y))) if Head(x) == 0 \
       else Push(1, Plus(Tail(x), Tail(y))) if Head(y) == 0 \
       else Push(0, Inc(Plus(Tail(x), Tail(y))))

Minus = lambda x, y: \
        x if y == Zeroes \
        else Inc(x) if y == Ones \
        else Push(Head(x), Minus(Tail(x), Tail(y))) if Head(y) == 0 \
        else Push(0, Minus(Tail(x), Tail(y))) if Head(x) == 1 \
        else Push(1, Dec(Minus(Tail(x), Tail(y))))

Negative = lambda x: Minus(Zeroes, x)

Mult = lambda x, y: \
       Zeroes if y == Zeroes \
       else Negative(x) if y == Ones \
       else Push(0, Mult(x, Tail(y))) if Head(y) == 0 \
       else Plus(x, Push(0, Mult(x, Tail(y))))

LessEqual = lambda x, y: \
            True if x == y or x == Ones and y == Zeroes \
            else False if x == Zeroes and y == Ones \
            else Less(Tail(x), Tail(y)) if Head(x) == 1 and Head(y) == 0 \
            else LessEqual(Tail(x),Tail(y))

Less = lambda x, y: \
       False if x == y or x == Zeroes and y == Ones \
       else True if x == Ones and y == Zeroes \
       else LessEqual(Tail(x), Tail(y)) if Head(x) == 0 and Head(y) == 1 \
       else Less(Tail(x),Tail(y))

Equal = lambda x, y: \
        x == Zeroes if y == Zeroes \
        else x == Ones if y == Ones \
        else Equal(Tail(x), Tail(y)) if Head(x) == Head(y) \
        else False

Zero = Zeroes
One = Inc(Zero)
Two = Inc(One)
Three = Inc(Two)
Four = Inc(Three)
Five = Inc(Four)
Six = Inc(Five)
Seven = Inc(Six)
Eight = Inc(Seven)
Nine = Inc(Eight)
Ten = Inc(Nine)

#check that 3 * (-5) == (-7) * 7 + 6 * 6 - 2
print(Equal(Mult(Three,
                 Negative(Five)),
            Minus(Plus(Mult(Negative(Seven),
                            Seven),
                       Mult(Six,
                            Six)),
                  Two)))

#check that -10 < -(3 * 3)
print(Less(Negative(Ten),
           Negative(Mult(Three,
                         Three))))


Output:
1
2
True
True


Create a new paste based on this one


Comments: