public static CTObject IsZero(CTObject arg)
        {
            AssertNumber(IsZeroFunctionName, arg);
            var res = Math.Abs(((CTNumber)arg).value) <= double.Epsilon;

            return(Constants.ToCTBool(res));
        }
        public override string DisplayObject()
        {
            StringBuilder repr = new StringBuilder("(");

            repr.Append(DisplayValue());
            CTObject tail = cdr;

            while (true)
            {
                if (tail.GetType() == typeof(CTPair))
                {
                    CTPair rest = (CTPair)tail;
                    repr.Append(" ").Append(rest.DisplayValue());
                    tail = rest.cdr;
                }
                else
                {
                    if (tail.GetType() != typeof(CTEmptyList))
                    {
                        repr.Append(" . ").Append(tail.DisplayObject());
                    }
                    break;
                }
            }

            repr.Append(")");

            return(repr.ToString());
        }
 private static void AssertNumber(string functionName, CTObject arg)
 {
     if (arg.GetType() != typeof(CTNumber))
     {
         throw new TypeError(functionName, CTNumber.TypeName, arg.DisplayType());
     }
 }
 protected override bool IsEqualTo(CTObject obj)
 {
     if (obj.GetType() == typeof(CTPair))
     {
         CTPair other = (CTPair)obj;
         return(other.car.Equals(car) && other.cdr.Equals(cdr));
     }
     else
     {
         return(false);
     }
 }
 public override CTObject apply5(CTObject a1, CTObject a2, CTObject a3, CTObject a4, CTObject a5)
 {
     if (isVarargs)
     {
         var args = new CTObject[] { a1, a2, a3, a4, a5 };
         return(funcallVarargs(args));
     }
     else
     {
         return(funcall5(a1, a2, a3, a4, a5));
     }
 }
 public override CTObject apply1(CTObject a1)
 {
     if (isVarargs)
     {
         var args = new CTObject[] { a1 };
         return(funcallVarargs(args));
     }
     else
     {
         return(funcall1(a1));
     }
 }
 public override CTObject apply3(CTObject a1, CTObject a2, CTObject a3)
 {
     if (isVarargs)
     {
         var args = new CTObject[] { a1, a2, a3 };
         return(funcallVarargs(args));
     }
     else
     {
         return(funcall3(a1, a2, a3));
     }
 }
 public override CTObject apply0()
 {
     if (isVarargs)
     {
         var args = new CTObject[0];
         return(funcallVarargs(args));
     }
     else
     {
         return(funcall0());
     }
 }
 public override CTObject apply4(CTObject a1, CTObject a2, CTObject a3, CTObject a4)
 {
     if (isVarargs)
     {
         var args = new CTObject[] { a1, a2, a3, a4 };
         return(funcallVarargs(args));
     }
     else
     {
         return(funcall4(a1, a2, a3, a4));
     }
 }
 public override CTObject apply2(CTObject a1, CTObject a2)
 {
     if (isVarargs)
     {
         var args = new CTObject[] { a1, a2 };
         return(funcallVarargs(args));
     }
     else
     {
         return(funcall2(a1, a2));
     }
 }
        public static CTObject Map(CTObject[] args)
        {
            AssertParameterCountAtLeast(MapFunctionName, 2, args);
            CTProcedure proc = args[0] as CTProcedure;

            if (proc == null)
            {
                throw new TypeError(MapFunctionName, CTProcedure.TypeName, args[0].DisplayType());
            }
            for (int i = 1; i < args.Length; ++i)
            {
                if (args[i].GetType() != typeof(CTPair))
                {
                    throw new TypeError(MapFunctionName, CTPair.TypeName, args[i].DisplayType());
                }
            }

            List <CTObject> results = new List <CTObject>();

            CTObject[] applyArgs      = new CTObject[args.Length - 1];
            bool       firstListEnded = false;

            while (!firstListEnded)
            {
                for (int i = 1; i < args.Length; ++i)
                {
                    if (args[i].GetType() == typeof(CTEmptyList))
                    {
                        firstListEnded = true;
                        break;
                    }
                    else if (args[i].GetType() == typeof(CTPair))
                    {
                        CTPair arg = (CTPair)args[i];
                        applyArgs[i - 1] = arg.car;
                        args[i]          = arg.cdr;
                    }
                    else
                    {
                        throw new TypeError(MapFunctionName, CTPair.TypeName, args[i].DisplayType());
                    }
                }

                if (!firstListEnded)
                {
                    results.Add(Apply(proc, applyArgs));
                }
            }

            return(List(results));
        }
 public override CTObject funcall1(CTObject a1)
 {
     return(fun1(a1));
 }
 public virtual CTObject funcall5(CTObject a1, CTObject a2, CTObject a3, CTObject a4, CTObject a5)
 {
     throw new InvalidNumberOfArgsError(GetName(), arity.ToString(), 5);
 }
 public virtual CTObject funcall2(CTObject a1, CTObject a2)
 {
     throw new InvalidNumberOfArgsError(GetName(), arity.ToString(), 2);
 }
 protected override bool IsEqualTo(CTObject obj)
 {
     return(obj == this);
 }
 protected override bool IsEqualTo(CTObject obj)
 {
     return(obj.GetType() == typeof(CTSymbol) && ((CTSymbol)obj).name.Equals(name));
 }
 protected abstract bool IsEqualTo(CTObject obj);
 public CTPair(CTObject v1, CTObject v2)
 {
     car = v1;
     cdr = v2;
 }
 protected override bool IsEqualTo(CTObject obj)
 {
     return(obj.GetType() == typeof(CTNumber) && ((CTNumber)obj).value == value);
 }
 protected override bool IsEqualTo(CTObject obj)
 {
     return(obj.GetType() == typeof(CTUndefined));
 }
 public virtual CTObject apply5(CTObject a1, CTObject a2, CTObject a3, CTObject a4, CTObject a5)
 {
     throw new NotAProcedureError(this);
 }
 public virtual CTObject apply2(CTObject a1, CTObject a2)
 {
     throw new NotAProcedureError(this);
 }
 public override CTObject funcall3(CTObject a1, CTObject a2, CTObject a3)
 {
     return(fun3(a1, a2, a3));
 }
 private static double getValue(CTObject obj)
 {
     return(((CTNumber)obj).value);
 }
 protected override bool IsEqualTo(CTObject obj)
 {
     return(obj.GetType() == typeof(CTEmptyList));
 }
 public NotAProcedureError(CTObject obj)
     : base(string.Format("object of type {0} is not a procedure\ngiven: {1}", obj.DisplayType(), obj))
 {
 }
 public override CTObject funcall2(CTObject a1, CTObject a2)
 {
     return(fun2(a1, a2));
 }
 protected override bool IsEqualTo(CTObject obj)
 {
     return(obj.GetType() == typeof(CTString) && ((CTString)obj).value.Equals(value));
 }
 public override CTObject funcall4(CTObject a1, CTObject a2, CTObject a3, CTObject a4)
 {
     return(fun4(a1, a2, a3, a4));
 }
 public static CTObject IsNull(CTObject v)
 {
     return(Constants.ToCTBool(v.GetType() == typeof(CTEmptyList)));
 }