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()); }
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 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)); }