There's always Lisp, in which 0 can be represented as ()
(the empty list), 1 is (())
, 2 is (() ())
, etc. This was proved way back in the day, but of course no Lisp implementation actually uses that, as it's too slow to believe.
+1
A:
JSBangs
2010-07-08 15:12:23
Sounds a bit like the set-theoretic definition of natural numbers.
jon hanson
2010-07-08 15:21:46
+2
A:
here a class that can be used for positive integers. and could easily be extended to account for negatives as well. it supports addition, subtraction, equality, inequality and multiplication. Division can be implmented based on the existing operators. So all in all I'd say it pretty much represents integers and there's no use of any simple types. Actually the only type the class uses is it self.
The implementation is basically a link list with some optimization so that traversing of the list ain't needed for every operation.
public class MyInt
{
private MyInt _previous;
private MyInt _first;
private bool isEven;
private MyInt(MyInt previous)
{
next++;
_previous = previous;
isEven = previous == null ? true : !previous.isEven;
getFirst();
x = next;
}
private void getFirst()
{
if (_previous == null)
_first = null;
else if (_previous._first == null)
_first = this;
else
_first = _previous._first;
}
private MyInt(MyInt copy, bool dontuse)
{
_previous = copy._previous == null ? null : new MyInt(copy._previous,true);
getFirst();
x = copy.x;
}
public static MyInt operator +(MyInt lhs, MyInt rhs)
{
if (object.ReferenceEquals(lhs, rhs))
rhs = new MyInt(rhs, true);
if (lhs == MyInt.Zero)
return rhs;
if (rhs == MyInt.Zero)
return lhs;
else
{
var isEven = rhs.isEven == lhs.isEven;
var result = new MyInt(rhs, true);
result._first._previous = lhs;
result._first = lhs._first;
result.isEven = isEven;
return result;
}
}
public static MyInt operator -(MyInt lhs, MyInt rhs)
{
if (lhs == rhs)
return MyInt.Zero;
if (rhs == MyInt.Zero)
return lhs;
if (lhs == MyInt.Zero)
throw new InvalidOperationException("Negatives not supported");
else
{
return lhs._previous - rhs._previous;
}
}
public static MyInt operator --(MyInt un)
{
if (un == MyInt.Zero)
throw new InvalidOperationException("Negatives not supported");
return un._previous;
}
public static MyInt operator *(MyInt lhs, MyInt rhs)
{
if (lhs == MyInt.Zero || rhs == MyInt.Zero)
return MyInt.Zero;
var temp = lhs;
var one = One;
var two = one + one;
var zero = MyInt.Zero;
var dbl = lhs + lhs;
if (rhs == MyInt.One)
return lhs;
if (rhs == two)
return dbl;
for (MyInt times = rhs + one; times._previous._previous != zero && times._previous != zero; times = times-two)
{
temp = temp + dbl;
}
if (rhs.isEven)
temp = temp - lhs;
return temp;
}
public static bool operator ==(MyInt lhs, MyInt rhs)
{
if (object.ReferenceEquals(lhs, null) && object.ReferenceEquals(rhs, null))
return true;
if ((object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null)))
return false;
if (object.ReferenceEquals(lhs._previous, null) && object.ReferenceEquals(rhs._previous, null))
return true;
if ((object.ReferenceEquals(lhs._previous, null) || object.ReferenceEquals(rhs._previous, null)))
return false;
return (lhs._previous == rhs._previous);
}
public static bool operator !=(MyInt lhs, MyInt rhs)
{
return !(lhs == rhs);
}
public override bool Equals(object obj)
{
return obj is MyInt && ((MyInt)obj) == this;
}
public static MyInt Zero
{
get
{
return new MyInt(null);
}
}
public static MyInt One
{
get
{
return new MyInt(new MyInt(null));
}
}
}
Rune FS
2010-07-08 21:33:03
This is a very interesting take on a list-based implementation! I also think `bool` is fine here, since it'd be trivially implemented in terms of referential types (null for false, non-null for true).
Jon Purdy
2010-07-09 01:23:46
basically the bool is just me being lazy. I needed a copy constructor but since I already had a constructor taking MyInt I needed a differrent signature (yes I know it's not pretty :) )
Rune FS
2010-07-09 06:36:46
Do'h you ment isEven of course and yes that could be implemented as null/not null or simply ommitted. It's an optimization for the multiplication operator
Rune FS
2010-07-09 06:38:59