PageRenderTime 72ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/sandbox/cheney/Sketch/src/org/plt/Kernel.java

http://github.com/cderici/moby-scheme
Java | 1611 lines | 1249 code | 311 blank | 51 comment | 132 complexity | 39577656b3029f7fd79b36ff9a950c21 MD5 | raw file
  1. package org.plt;
  2. // The Kernel class contains all of the builtins except for the world
  3. // primitives, which lives under the j2me tree.
  4. import java.util.Calendar;
  5. import org.plt.checker.*;
  6. import org.plt.types.*;
  7. import net.dclausen.microfloat.*;
  8. import org.plt.types.Bignum;
  9. public class Kernel {
  10. private static Class stringClass;
  11. private static Class characterClass;
  12. private static Class listClass;
  13. private static Class pairClass;
  14. static {
  15. // Workaround bug in CLDC 1.0. Bug 4313427 doesn't allow us to use
  16. // java.lang.String.class directly.
  17. // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4313427
  18. try {
  19. stringClass = Class.forName("java.lang.String");
  20. characterClass = Class.forName("java.lang.Character");
  21. listClass = Class.forName("org.plt.types.List");
  22. pairClass = Class.forName("org.plt.types.Pair");
  23. } catch (ClassNotFoundException e) {
  24. throw new SchemeException(e);
  25. }
  26. }
  27. static private java.util.Random randgen = new java.util.Random();
  28. static public org.plt.types.Number ZERO = Rational.ZERO;
  29. static public org.plt.types.Number ONE = Rational.ONE;
  30. static public org.plt.types.Number TWO = NumberTower.plus(ONE, ONE);
  31. static public org.plt.types.Number HALF = NumberTower.divide(ONE, TWO);
  32. static public org.plt.types.Number THREE = NumberTower.plus(ONE, TWO);
  33. static public org.plt.types.Number FOUR = NumberTower.plus(ONE, THREE);
  34. static public org.plt.types.Number FIVE = NumberTower.plus(ONE, FOUR);
  35. static public org.plt.types.Number SIX =
  36. NumberTower.multiply(TWO, THREE);
  37. // no-op: void -> void
  38. public static Object no_op() {
  39. return null;
  40. }
  41. public static Object no_op_worldEvent(Object world) {
  42. return world;
  43. }
  44. public static Object no_op_stopWhen(Object world) {
  45. return Logic.FALSE;
  46. }
  47. public static Object no_op_keyEvent(Object world, Object key) {
  48. return world;
  49. }
  50. public static Object no_op_messageEvent(Object world, Object aMessage) {
  51. return world;
  52. }
  53. public static Object no_op_locationChangeEvent(Object world, Object latitude, Object longitude) {
  54. return world;
  55. }
  56. // ////////////////////////////////////////////////////////////////////
  57. public static org.plt.types.Number pi = FloatPoint.PI;
  58. public static org.plt.types.Number e = FloatPoint.E;
  59. public static Object identity(Object x) {
  60. return x;
  61. }
  62. private static Logic chainedTest(ChainingTester c,
  63. Object firstArg,
  64. Object secondArg,
  65. Object[] restArgs) {
  66. if (! c.test(firstArg, secondArg))
  67. return Logic.FALSE;
  68. if (restArgs.length > 0 && (! c.test(secondArg, restArgs[0])))
  69. return Logic.FALSE;
  70. for(int i = 0; i < restArgs.length - 1; i++) {
  71. if (! c.test(restArgs[i], restArgs[i+1]))
  72. return Logic.FALSE;
  73. }
  74. return Logic.TRUE;
  75. }
  76. private static interface ChainingTester {
  77. public boolean test(Object x, Object j);
  78. }
  79. // Numerics
  80. // >=
  81. public static Logic _greaterthan__equal_(Object firstArg,
  82. Object secondArg,
  83. Object[] restArgs) {
  84. return chainedTest(new ChainingTester() {
  85. public boolean test(Object x, Object y) {
  86. return NumberTower.greaterThanEqual
  87. ((org.plt.types.Number) x,
  88. (org.plt.types.Number) y);
  89. }
  90. },
  91. firstArg, secondArg, restArgs);
  92. }
  93. // >
  94. public static Logic _greaterthan_(Object firstArg,
  95. Object secondArg,
  96. Object[] restArgs) {
  97. return chainedTest(new ChainingTester() {
  98. public boolean test(Object x, Object y) {
  99. return NumberTower.greaterThan
  100. ((org.plt.types.Number) x,
  101. (org.plt.types.Number) y);
  102. }
  103. },
  104. firstArg, secondArg, restArgs);
  105. }
  106. // <=
  107. public static Logic _lessthan__equal_(Object firstArg,
  108. Object secondArg,
  109. Object[] restArgs) {
  110. return chainedTest(new ChainingTester() {
  111. public boolean test(Object x, Object y) {
  112. return NumberTower.lessThanEqual
  113. ((org.plt.types.Number) x,
  114. (org.plt.types.Number) y);
  115. }
  116. },
  117. firstArg, secondArg, restArgs);
  118. }
  119. // <
  120. public static Logic _lessthan_(Object firstArg,
  121. Object secondArg,
  122. Object[] restArgs) {
  123. return chainedTest(new ChainingTester() {
  124. public boolean test(Object x, Object y) {
  125. return NumberTower.lessThan
  126. ((org.plt.types.Number) x,
  127. (org.plt.types.Number) y);
  128. }
  129. },
  130. firstArg, secondArg, restArgs);
  131. }
  132. // =
  133. public static Logic _equal_(Object firstArg,
  134. Object secondArg,
  135. Object[] restArgs) {
  136. return chainedTest(new ChainingTester() {
  137. public boolean test(Object x, Object y) {
  138. return NumberTower.equal
  139. ((org.plt.types.Number) x,
  140. (org.plt.types.Number) y);
  141. }
  142. },
  143. firstArg, secondArg, restArgs);
  144. }
  145. // =~
  146. public static Logic _equal__tilde_(Object _n1, Object _n2, Object _n3) {
  147. return toLogic(NumberTower.approxEqual
  148. ((org.plt.types.Number) _n1,
  149. (org.plt.types.Number) _n2,
  150. (org.plt.types.Number) _n3));
  151. }
  152. // +
  153. public static org.plt.types.Number _plus_(Object[] args) {
  154. org.plt.types.Number currentSum = ZERO;
  155. for(int i = 0; i < args.length; i++) {
  156. currentSum = NumberTower.plus
  157. (currentSum,
  158. (org.plt.types.Number) args[i]);
  159. }
  160. return currentSum;
  161. }
  162. // -
  163. public static org.plt.types.Number _dash_(Object firstArg,
  164. Object[] args) {
  165. if (args.length == 0) {
  166. return NumberTower.minus(Rational.ZERO,
  167. (org.plt.types.Number) firstArg);
  168. }
  169. org.plt.types.Number currentDifference =
  170. (org.plt.types.Number ) firstArg;
  171. for(int i = 0; i < args.length; i++) {
  172. currentDifference = NumberTower.minus
  173. (currentDifference,
  174. (org.plt.types.Number) args[i]);
  175. }
  176. return currentDifference;
  177. }
  178. // *
  179. public static org.plt.types.Number _star_(Object[] args) {
  180. org.plt.types.Number currentProduct = ONE;
  181. for (int i = 0; i < args.length; i++) {
  182. currentProduct =
  183. NumberTower.multiply((org.plt.types.Number) args[i],
  184. currentProduct);
  185. }
  186. return currentProduct;
  187. }
  188. // /
  189. public static org.plt.types.Number _slash_(Object x, Object[] args) {
  190. org.plt.types.Number currentDivision =
  191. (org.plt.types.Number) x;
  192. for (int i = 0; i < args.length; i++) {
  193. currentDivision = NumberTower.divide(currentDivision,
  194. (org.plt.types.Number) args[i]);
  195. }
  196. return currentDivision;
  197. }
  198. public static org.plt.types.Number abs(Object n) {
  199. return ((org.plt.types.Number) n).abs();
  200. }
  201. public static org.plt.types.Number acos(Object n) {
  202. return ((org.plt.types.Number) n).acos();
  203. }
  204. public static org.plt.types.Number sqrt(Object _n) {
  205. org.plt.types.Number n = (org.plt.types.Number) _n;
  206. return ((org.plt.types.Number) n).sqrt();
  207. }
  208. public static org.plt.types.Number modulo(Object _n1, Object _n2) {
  209. org.plt.types.Number n1 = (org.plt.types.Number) _n1;
  210. org.plt.types.Number n2 = (org.plt.types.Number) _n2;
  211. if (NumberTower.coerseLeft(n1, ONE) != null) {
  212. n1 = NumberTower.coerseLeft(n1, ONE);
  213. }
  214. if (NumberTower.coerseLeft(n2, ONE) != null) {
  215. n2 = NumberTower.coerseLeft(n2, ONE);
  216. }
  217. return n1.modulo(n2);
  218. }
  219. public static org.plt.types.Number floor(Object _n1) {
  220. return ((org.plt.types.Number) _n1).floor();
  221. }
  222. public static org.plt.types.Number ceiling(Object _n1) {
  223. return ((org.plt.types.Number) _n1).ceiling();
  224. }
  225. public static org.plt.types.Number sin(Object _n1) {
  226. return ((org.plt.types.Number) _n1).sin();
  227. }
  228. public static org.plt.types.Number asin(Object _n1) {
  229. return ((org.plt.types.Number) _n1).asin();
  230. }
  231. public static org.plt.types.Number atan(Object _n1) {
  232. return ((org.plt.types.Number) _n1).atan();
  233. }
  234. public static org.plt.types.Number cos(Object _n1) {
  235. return ((org.plt.types.Number) _n1).cos();
  236. }
  237. public static String number_dash__greaterthan_string(Object _n1) {
  238. return ((org.plt.types.Number) _n1).toString();
  239. }
  240. public static Logic equal_question_(Object _o1, Object _o2) {
  241. if (_o1 instanceof org.plt.types.Number
  242. && _o2 instanceof org.plt.types.Number) {
  243. return toLogic(NumberTower.equal((org.plt.types.Number)_o1,
  244. (org.plt.types.Number)_o2));
  245. }
  246. return toLogic(_o1.equals(_o2));
  247. }
  248. // ////////////////////////////////////////////////////////////////////
  249. public static org.plt.types.Number random(Object n) {
  250. int result = randgen.nextInt() % ((org.plt.types.Number) n).toInt();
  251. // Oddity, but modulo can return negative if dividend is negative.
  252. if (result < 0) {
  253. return new Rational(result + ((org.plt.types.Number) n).toInt(), 1);
  254. } else {
  255. return new Rational(result, 1);
  256. }
  257. }
  258. public static Logic zero_question_(java.lang.Object n) {
  259. return toLogic(((org.plt.types.Number) n).isZero());
  260. }
  261. public static org.plt.types.Number max(Object firstArg,
  262. Object[] restArgs) {
  263. org.plt.types.Number currentMax = (org.plt.types.Number) firstArg;
  264. for(int i = 0; i < restArgs.length; i++) {
  265. currentMax =
  266. NumberTower.greaterThanEqual
  267. (currentMax,
  268. (org.plt.types.Number) restArgs[i])
  269. ? currentMax : (org.plt.types.Number) restArgs[i];
  270. }
  271. return currentMax;
  272. }
  273. public static org.plt.types.Number min(Object firstArg,
  274. Object[] restArgs) {
  275. org.plt.types.Number currentMin = (org.plt.types.Number) firstArg;
  276. for(int i = 0; i < restArgs.length; i++) {
  277. currentMin =
  278. NumberTower.lessThanEqual
  279. (currentMin,
  280. (org.plt.types.Number) restArgs[i])
  281. ? currentMin : (org.plt.types.Number) restArgs[i];
  282. }
  283. return currentMin;
  284. }
  285. public static org.plt.types.Number sqr(Object n) {
  286. return NumberTower.multiply((org.plt.types.Number)n,
  287. (org.plt.types.Number)n);
  288. }
  289. public static org.plt.types.Number add1(Object n) {
  290. return NumberTower.plus((org.plt.types.Number) n, ONE);
  291. }
  292. public static org.plt.types.Number sub1(Object n) {
  293. return NumberTower.minus((org.plt.types.Number) n, ONE);
  294. }
  295. // ////////////////////////////////////////////////////////////////////
  296. public static Logic string_equal__question_(Object firstArg, Object secondArg,
  297. Object[] args) {
  298. ArgumentChecker.checkArrayType(args, stringClass, "string=?");
  299. return chainedTest(new ChainingTester() {
  300. public boolean test(Object x, Object y) {
  301. return (((String) x).equals((String) y));
  302. }
  303. },
  304. firstArg, secondArg, args);
  305. // for (int i = 0; i < args.length - 1; i++) {
  306. // String s1 = (String) args[i];
  307. // String s2 = (String) args[i+1];
  308. // if (string_question_(s1).isFalse())
  309. // throw new SchemeException(
  310. // "string=?: expects type <string> as 1st argument, given: "
  311. // + s1 + "; other arguments were: " + s2);
  312. // if (string_question_(s1).isFalse())
  313. // throw new SchemeException(
  314. // "string=?: expects type <string> as 2nd argument, given: "
  315. // + s2 + "; other arguments were: " + s1);
  316. // if (! s1.equals(s2)) {
  317. // return Logic.FALSE;
  318. // }
  319. // }
  320. // return Logic.TRUE;
  321. }
  322. public static Logic struct_question_(Object obj) {
  323. return toLogic(obj instanceof org.plt.types.Struct);
  324. }
  325. // ////////////////////////////////////////////////////////////////////
  326. // Posn stuff
  327. public static Posn make_dash_posn(Object x, Object y) {
  328. return new Posn(x, y);
  329. }
  330. public static Object posn_dash_x(Object p) {
  331. return ((Posn) p).getX();
  332. }
  333. public static Object posn_dash_y(Object p) {
  334. return ((Posn) p).getY();
  335. }
  336. public static Logic posn_question_(Object p) {
  337. return toLogic(p instanceof org.plt.types.Posn);
  338. }
  339. // ////////////////////////////////////////////////////////////////////
  340. // ////////////////////////////////////////////////////////////////////
  341. public static Object first(Object l) {
  342. return ((org.plt.types.List) l).first();
  343. }
  344. public static Object second(Object _l) {
  345. org.plt.types.List l = (org.plt.types.List) _l;
  346. return l.rest().first();
  347. }
  348. public static Object third(Object _l) {
  349. org.plt.types.List l = (org.plt.types.List) _l;
  350. return l.rest().rest().first();
  351. }
  352. public static Object fourth(Object _l) {
  353. org.plt.types.List l = (org.plt.types.List) _l;
  354. return l.rest().rest().rest().first();
  355. }
  356. public static Object fifth(Object _l) {
  357. org.plt.types.List l = (org.plt.types.List) _l;
  358. return l.rest().rest().rest().rest().first();
  359. }
  360. public static Object sixth(Object _l) {
  361. org.plt.types.List l = (org.plt.types.List) _l;
  362. return l.rest().rest().rest().rest().rest().first();
  363. }
  364. public static Object seventh(Object _l) {
  365. org.plt.types.List l = (org.plt.types.List) _l;
  366. return l.rest().rest().rest().rest().rest().rest().first();
  367. }
  368. public static Object eighth(Object _l) {
  369. org.plt.types.List l = (org.plt.types.List) _l;
  370. return l.rest().rest().rest().rest().rest().rest().rest().first();
  371. }
  372. public static org.plt.types.List reverse(Object _l) {
  373. org.plt.types.List l = (org.plt.types.List) _l;
  374. org.plt.types.List rev = org.plt.types.Empty.EMPTY;
  375. while (!l.isEmpty()) {
  376. rev = cons(l.first(), rev);
  377. l = l.rest();
  378. }
  379. return rev;
  380. }
  381. public static org.plt.types.List rest(Object l) {
  382. return ((org.plt.types.List) l).rest();
  383. }
  384. public static Object car(Object o) {
  385. return first(o);
  386. }
  387. public static org.plt.types.List cdr(Object o) {
  388. return rest(o);
  389. }
  390. public static Object caaar(Object o) {
  391. return car(car(car(o)));
  392. }
  393. public static Object caadr(Object o) {
  394. return car(car(cdr(o)));
  395. }
  396. public static Object caar(Object o) {
  397. return car(car(o));
  398. }
  399. public static Object cadar(Object o) {
  400. return car(cdr(car(o)));
  401. }
  402. public static Object cadddr(Object o) {
  403. return car(cdr(cdr(cdr(o))));
  404. }
  405. public static Object caddr(Object o) {
  406. return car(cdr(cdr(o)));
  407. }
  408. public static Object cadr(Object o) {
  409. return car(cdr(o));
  410. }
  411. public static org.plt.types.List cdaar(Object o) {
  412. return cdr(car(car(o)));
  413. }
  414. public static org.plt.types.List cdadr(Object o) {
  415. return cdr(car(cdr(o)));
  416. }
  417. public static org.plt.types.List cdar(Object o) {
  418. return cdr(car(o));
  419. }
  420. public static org.plt.types.List cddar(Object o) {
  421. return cdr(cdr(car(o)));
  422. }
  423. public static org.plt.types.List cdddr(Object o) {
  424. return cdr(cdr(cdr(o)));
  425. }
  426. public static org.plt.types.List cddr(Object o) {
  427. return cdr(cdr(o));
  428. }
  429. public static Logic empty_question_(Object l) {
  430. return toLogic(((org.plt.types.List) l).isEmpty());
  431. }
  432. public static org.plt.types.List cons(Object x, Object xs) {
  433. return new Pair(x, (org.plt.types.List) xs);
  434. }
  435. // ////////////////////////////////////////////////////////////////////
  436. // ////////////////////////////////////////////////////////////////////
  437. public static Logic symbol_equal__question_(Object s1, Object s2) {
  438. return toLogic(((Symbol) s1).equals(s2));
  439. }
  440. public static String symbol_dash__greaterthan_string(Object o) {
  441. return ((Symbol) o).toString();
  442. }
  443. public static Logic not(Object l) {
  444. return ((Logic) l).negate();
  445. }
  446. // ////////////////////////////////////////////////////////////////////
  447. public static Object error(Object s, Object msg) {
  448. throw new SchemeException(s + ": " + msg);
  449. }
  450. public static Object error(Object s) {
  451. throw new SchemeException("" + s);
  452. }
  453. // ////////////////////////////////////////////////////////////////////
  454. // Converts from boolean to Logics.
  455. private static Logic toLogic(boolean b) {
  456. return b ? Logic.TRUE : Logic.FALSE;
  457. }
  458. public static org.plt.types.Number tan(Object _n1) {
  459. return NumberTower.divide(sin(_n1), cos(_n1));
  460. }
  461. public static org.plt.types.Number sinh(Object _n1) {
  462. return NumberTower.divide(NumberTower.minus
  463. (exp((org.plt.types.Number) _n1),
  464. exp(NumberTower.minus(ZERO,
  465. (org.plt.types.Number)_n1))),
  466. TWO);
  467. }
  468. public static org.plt.types.Number cosh(Object _n1) {
  469. return NumberTower.divide(NumberTower.plus(exp(_n1), exp(NumberTower.minus(ZERO, (org.plt.types.Number)_n1))), TWO);
  470. }
  471. public static org.plt.types.Logic even_question_(Object n) {
  472. if (integer_question_(n).isFalse())
  473. throw new SchemeException(
  474. "even?: expects argument of type <integer>; given " + n);
  475. return zero_question_(modulo(n, TWO));
  476. }
  477. public static org.plt.types.Logic odd_question_(Object n) {
  478. if (integer_question_(n).isFalse())
  479. throw new SchemeException(
  480. "odd?: expects argument of type <integer>; given: " + n);
  481. return not(even_question_(n));
  482. }
  483. public static org.plt.types.Number expt(Object n1, Object n2) {
  484. if (number_question_(n1).isFalse())
  485. throw new SchemeException(
  486. "expt: expects type <number> as 1st argument, given: " + n1
  487. + "; other arguments were: " + n2);
  488. if (number_question_(n2).isFalse())
  489. throw new SchemeException(
  490. "expt: expects type <number> as 2nd argument, given: " + n2
  491. + "; other arguments were: " + n1);
  492. return (FloatPoint.fromNumber((org.plt.types.Number) n1))
  493. .expt((org.plt.types.Number) n2);
  494. }
  495. public static org.plt.types.Number exp(Object exponent) {
  496. if (number_question_(exponent).isFalse())
  497. throw new SchemeException(
  498. "exp: expects argument of type <number>; given " + exponent);
  499. return expt(e, exponent);
  500. }
  501. public static org.plt.types.Number log(Object n1) {
  502. if (number_question_(n1).isFalse())
  503. throw new SchemeException(
  504. "log: expects argument of type <number>; given: " + n1);
  505. return (FloatPoint.fromNumber((org.plt.types.Number) n1)).log();
  506. }
  507. public static org.plt.types.Logic positive_question_(Object n1) {
  508. if (number_question_(n1).isFalse())
  509. throw new SchemeException(
  510. "positive?: expects argument of type <real number>; given: "
  511. + n1);
  512. return toLogic(NumberTower.greaterThan((org.plt.types.Number)n1, ZERO));
  513. }
  514. public static org.plt.types.Logic negative_question_(Object n1) {
  515. if (number_question_(n1).isFalse())
  516. throw new SchemeException(
  517. "negative?: expects argument of type <real number>; given: "
  518. + n1);
  519. return toLogic(NumberTower.lessThan((org.plt.types.Number)n1, ZERO));
  520. }
  521. public static org.plt.types.Number sgn(Object n1) {
  522. if (number_question_(n1).isFalse())
  523. throw new SchemeException("sgn: expects argument of type <number>");
  524. if (positive_question_(n1).isTrue())
  525. return (org.plt.types.Number) ONE;
  526. if (negative_question_(n1).isTrue())
  527. return (org.plt.types.Number) (NumberTower.minus(ZERO, ONE));
  528. return ZERO;
  529. }
  530. public static org.plt.types.Logic boolean_question_(Object n1) {
  531. return toLogic(n1 instanceof org.plt.types.Logic);
  532. }
  533. public static org.plt.types.Logic false_question_(Object n1) {
  534. return ((org.plt.types.Logic) n1).negate();
  535. }
  536. public static org.plt.types.Logic boolean_equal__question_(Object n1,
  537. Object n2) {
  538. if (boolean_question_(n1).isFalse())
  539. throw new SchemeException(
  540. "boolean=?: expects type <boolean> as 1st argument, given: "
  541. + n1 + "; other arguments were: " + n2);
  542. if (boolean_question_(n2).isFalse())
  543. throw new SchemeException(
  544. "boolean=?: expects type <boolean> as 2nt argument, given: "
  545. + n2 + "; other arguments were: " + n1);
  546. return toLogic(((org.plt.types.Logic) n1).isTrue() == ((org.plt.types.Logic) n2)
  547. .isTrue());
  548. }
  549. public static org.plt.types.Logic symbol_question_(Object n) {
  550. return toLogic(n instanceof org.plt.types.Symbol);
  551. }
  552. public static org.plt.types.Number gcd(Object first,
  553. Object second,
  554. Object[] args) {
  555. org.plt.types.Number currentGcd =
  556. gcd2((org.plt.types.Number) first,
  557. (org.plt.types.Number) second);
  558. for(int i = 0; i < args.length; i++) {
  559. currentGcd = gcd2(currentGcd,
  560. gcd2(currentGcd,
  561. (org.plt.types.Number) args[i]));
  562. }
  563. return currentGcd;
  564. }
  565. private static org.plt.types.Number gcd2(org.plt.types.Number a,
  566. org.plt.types.Number b) {
  567. if (integer_question_(a).isFalse())
  568. throw new SchemeException(
  569. "gcd: expects type <integer> as 1st argument; giving: " + a);
  570. if (integer_question_(b).isFalse())
  571. throw new SchemeException(
  572. "gcd: expects type <integer> as 2nd argument; giving: " + b);
  573. if (negative_question_(a).isTrue())
  574. a = NumberTower.minus(ZERO, (org.plt.types.Number) a);
  575. if (negative_question_(b).isTrue())
  576. b = NumberTower.minus(ZERO, (org.plt.types.Number) b);
  577. while (! NumberTower.equal(b, ZERO)) {
  578. org.plt.types.Number t = (org.plt.types.Number) b;
  579. b = ((org.plt.types.Number) a).modulo((org.plt.types.Number) b);
  580. a = t;
  581. }
  582. return (org.plt.types.Number) a;
  583. }
  584. public static org.plt.types.Number lcm(Object first, Object second,
  585. Object[] args) {
  586. org.plt.types.Number commonDivisor = gcd(first, second, args);
  587. org.plt.types.Number product =
  588. NumberTower.multiply((org.plt.types.Number)second,
  589. NumberTower.multiply
  590. ((org.plt.types.Number)first,
  591. _star_(args)));
  592. return abs(NumberTower.divide(product, commonDivisor));
  593. }
  594. public static org.plt.types.Logic pair_question_(Object n) {
  595. return toLogic(n instanceof org.plt.types.Pair);
  596. }
  597. public static org.plt.types.Logic cons_question_(Object n) {
  598. return toLogic(n instanceof org.plt.types.Pair);
  599. }
  600. public static org.plt.types.Logic number_question_(Object n) {
  601. return toLogic(n instanceof org.plt.types.Number);
  602. }
  603. public static org.plt.types.Logic rational_question_(Object n) {
  604. return toLogic(n instanceof org.plt.types.Rational);
  605. }
  606. public static org.plt.types.Number quotient(Object a, Object b) {
  607. if (integer_question_(a).isFalse())
  608. throw new SchemeException(
  609. "quotient: expects type <integer> as 1st argument, given: "
  610. + a);
  611. if (integer_question_(b).isFalse())
  612. throw new SchemeException(
  613. "quotient: expects type <integer> as 2nd argument, given: "
  614. + b);
  615. return floor(NumberTower.divide((org.plt.types.Number)a,
  616. (org.plt.types.Number)b));
  617. }
  618. public static org.plt.types.Number remainder(Object a, Object b) {
  619. if (integer_question_(a).isFalse())
  620. throw new SchemeException(
  621. "remainder: expects type <integer> as 1st argument, given: "
  622. + a);
  623. if (integer_question_(b).isFalse())
  624. throw new SchemeException(
  625. "remainder: expects type <integer> as 2nd argument, given: "
  626. + b);
  627. return NumberTower.minus((org.plt.types.Number)a, NumberTower.multiply
  628. (quotient((org.plt.types.Number)a,
  629. (org.plt.types.Number)b),
  630. (org.plt.types.Number) b));
  631. }
  632. public static org.plt.types.Number numerator(Object n) {
  633. if (rational_question_(n).isFalse())
  634. throw new SchemeException(
  635. "numerator: expects argument of type <rational number>, giving: "
  636. + n);
  637. return new Rational(((org.plt.types.Rational) n).numerator(),
  638. Bignum.ONE);
  639. }
  640. public static org.plt.types.Number denominator(Object n) {
  641. if (rational_question_(n).isFalse())
  642. throw new SchemeException(
  643. "denominator: expects argument of type <rational number>, giving: "
  644. + n);
  645. return new Rational(((org.plt.types.Rational) n).denominator(),
  646. Bignum.ONE);
  647. }
  648. public static org.plt.types.Logic integer_question_(Object n) {
  649. return toLogic(NumberTower.equal(Kernel.denominator(n), ONE));
  650. }
  651. public static org.plt.types.Logic null_question_(Object n) {
  652. return toLogic(((org.plt.types.List) n).isEmpty());
  653. }
  654. public static org.plt.types.Number length(Object n) {
  655. ArgumentChecker.checkAtomType(n, "org.plt.types.List", "length");
  656. // if (cons_question_(n).isFalse())
  657. // throw new SchemeException(
  658. // "length: expects argument of type <proper list>; given: "
  659. // + n);
  660. org.plt.types.Number len = ZERO;
  661. while (((org.plt.types.List) n).isEmpty() == false) {
  662. len = add1(len);
  663. n = ((org.plt.types.List) n).rest();
  664. }
  665. return len;
  666. }
  667. public static Object list_dash_ref(Object lst, Object i) {
  668. if (negative_question_(i).isTrue())
  669. throw new SchemeException(
  670. "list-ref: expects type <non-negative exact integer> as 2nd argument, given: "
  671. + i + "; other arguments were: " + lst);
  672. org.plt.types.Number len = length(lst);
  673. if (NumberTower.greaterThanEqual((org.plt.types.Number) i,
  674. (org.plt.types.Number) len))
  675. throw new SchemeException("list-ref: index " + i
  676. + " too large for " + lst);
  677. org.plt.types.Number index = ZERO;
  678. while (NumberTower.lessThan(index, (org.plt.types.Number) i)) {
  679. index = add1(index);
  680. lst = ((org.plt.types.List) lst).rest();
  681. }
  682. return ((org.plt.types.List) lst).first();
  683. }
  684. public static org.plt.types.Number round(Object n) {
  685. if (real_question_(n).isFalse())
  686. throw new SchemeException(
  687. "round: expects argument of type <real number>; given: "
  688. + n);
  689. if (negative_question_(n).isFalse()) {
  690. if (NumberTower.lessThan(NumberTower.minus((org.plt.types.Number)n,
  691. floor(n)),
  692. HALF))
  693. return floor(n);
  694. else
  695. return ceiling(n);
  696. } else {
  697. return NumberTower.minus(ZERO, round(abs(n)));
  698. }
  699. }
  700. public static org.plt.types.Logic real_question_(Object n) {
  701. return (n instanceof org.plt.types.Number ? toLogic(((org.plt.types.Number) n)
  702. .isReal())
  703. : Logic.FALSE);
  704. }
  705. public static org.plt.types.Logic string_question_(Object n) {
  706. return toLogic(n instanceof String);
  707. }
  708. public static org.plt.types.Logic string_greaterthan__question_
  709. (Object firstArg, Object secondArg, Object[] restArgs) {
  710. ArgumentChecker.checkArraySize(restArgs, 0, "string>?");
  711. ArgumentChecker.checkArrayType(restArgs, stringClass, "string>?");
  712. return chainedTest(new ChainingTester() {
  713. public boolean test(Object n1, Object n2) {
  714. return (((String) n1).compareTo((String) n2) > 0);
  715. }
  716. },
  717. firstArg, secondArg, restArgs);
  718. }
  719. public static org.plt.types.Logic string_greaterthan__equal__question_
  720. (Object firstArg, Object secondArg, Object[] restArgs) {
  721. ArgumentChecker.checkArraySize(restArgs, 0, "string>=?");
  722. ArgumentChecker.checkArrayType(restArgs, stringClass, "string>=?");
  723. return chainedTest(new ChainingTester() {
  724. public boolean test(Object n1, Object n2) {
  725. return (((String) n1).compareTo((String) n2) >= 0);
  726. }
  727. },
  728. firstArg, secondArg, restArgs);
  729. }
  730. public static org.plt.types.Logic string_lessthan__question_
  731. (Object firstArg, Object secondArg, Object[] restArgs) {
  732. ArgumentChecker.checkArraySize(restArgs, 0, "string<?");
  733. ArgumentChecker.checkArrayType(restArgs, stringClass, "string<?");
  734. return chainedTest(new ChainingTester() {
  735. public boolean test(Object n1, Object n2) {
  736. return (((String) n1).compareTo((String) n2) < 0);
  737. }
  738. },
  739. firstArg, secondArg, restArgs);
  740. }
  741. public static org.plt.types.Logic string_lessthan__equal__question_
  742. (Object firstArg, Object secondArg, Object[] restArgs) {
  743. ArgumentChecker.checkArraySize(restArgs, 0, "string<=?");
  744. ArgumentChecker.checkArrayType(restArgs, stringClass, "string<=?");
  745. return chainedTest(new ChainingTester() {
  746. public boolean test(Object n1, Object n2) {
  747. return (((String) n1).compareTo((String) n2) <= 0);
  748. }
  749. },
  750. firstArg, secondArg, restArgs);
  751. }
  752. private static org.plt.types.Logic natural_question_(Object n) {
  753. return toLogic(integer_question_(n).isTrue()
  754. && negative_question_(n).isFalse());
  755. }
  756. public static Object substring(Object s, Object begin, Object end) {
  757. if (string_question_(s).isFalse())
  758. throw new SchemeException(
  759. "substring: expects type <string> as 1st argument, given: "
  760. + s + "; other arguments were: " + begin + " "
  761. + end);
  762. if (natural_question_(begin).isFalse())
  763. throw new SchemeException(
  764. "substring: expects type <non-negative exact integer> as 2nd argument, given: "
  765. + begin + "; other arguments were: " + s + " "
  766. + end);
  767. if (natural_question_(end).isFalse())
  768. throw new SchemeException(
  769. "substring: expects type <non-negative exact integer> as 3rd argument, given: "
  770. + end + "; other arguments were: " + s + " "
  771. + begin);
  772. return ((String) s).substring(((Rational) begin).toInt(),
  773. ((Rational) end).toInt());
  774. }
  775. public static Object string_dash_ref(Object s, Object i) {
  776. if (string_question_(s).isFalse())
  777. throw new SchemeException(
  778. "string-ref: expects type <string> as 1st argument, given: "
  779. + s + "; other arguments were: " + i);
  780. if (natural_question_(i).isFalse())
  781. throw new SchemeException(
  782. "string-ref: expects type <non-negative exact integer> as 2nd argument, given: "
  783. + i + "; other arguments were: " + s);
  784. if (NumberTower.greaterThan((org.plt.types.Number)i,
  785. sub1(string_dash_length(s))))
  786. throw new SchemeException("string-ref: index " + i
  787. + " out of range [0, " + sub1(string_dash_length(s))
  788. + "] for string: " + s);
  789. return new Character(((String) s).charAt(((Rational) i).toInt()));
  790. }
  791. public static org.plt.types.Number string_dash_length(Object s) {
  792. if (string_question_(s).isFalse())
  793. throw new SchemeException(
  794. "string-length: expects argument of type <string>; given "
  795. + s);
  796. return new Rational(((String) s).length(), 1);
  797. }
  798. public static Object string_dash_copy(Object s) {
  799. if (string_question_(s).isFalse())
  800. throw new SchemeException(
  801. "string-copy: expects argument of type <string>; given "
  802. + s);
  803. return new String((String) s);
  804. }
  805. public static Object string_dash__greaterthan_number(Object s) {
  806. if (string_question_(s).isFalse())
  807. throw new SchemeException(
  808. "string->number: expects argument of type <string>; given "
  809. + s);
  810. try {
  811. return new Rational(Integer.parseInt((String) s), 1);
  812. } catch (NumberFormatException e1) {
  813. try {
  814. return FloatPoint.fromString((String) s);
  815. } catch (NumberFormatException e2) {
  816. return Logic.FALSE;
  817. }
  818. }
  819. }
  820. public static org.plt.types.Logic eq_question_(Object n1, Object n2) {
  821. return toLogic(n1 == n2);
  822. }
  823. public static org.plt.types.Logic eqv_question_(Object n1, Object n2) {
  824. return toLogic(n1 == n2);
  825. }
  826. public static org.plt.types.Logic char_question_(Object n) {
  827. return toLogic(n instanceof Character);
  828. }
  829. public static org.plt.types.Logic char_equal__question_(Object n1,
  830. Object n2) {
  831. return toLogic(((Character) n1).charValue() == ((Character) n2)
  832. .charValue());
  833. }
  834. public static org.plt.types.Logic char_lessthan__question_
  835. (Object firstArg, Object secondArg, Object[] restArgs) {
  836. ArgumentChecker.checkArraySize(restArgs, 0, "char<?");
  837. ArgumentChecker.checkArrayType(restArgs, characterClass, "char<?");
  838. return chainedTest(new ChainingTester() {
  839. public boolean test(Object n1, Object n2) {
  840. return (((Character) n1).charValue() <
  841. ((Character) n2).charValue());
  842. }
  843. },
  844. firstArg, secondArg, restArgs);
  845. }
  846. public static org.plt.types.Logic char_lessthan__equal__question_
  847. (Object firstArg, Object secondArg, Object[] restArgs) {
  848. ArgumentChecker.checkArraySize(restArgs, 0, "char<=?");
  849. ArgumentChecker.checkArrayType(restArgs, characterClass, "char<=?");
  850. return chainedTest(new ChainingTester() {
  851. public boolean test(Object n1, Object n2) {
  852. return (((Character) n1).charValue() <=
  853. ((Character) n2).charValue());
  854. }
  855. },
  856. firstArg, secondArg, restArgs);
  857. }
  858. public static org.plt.types.Logic char_greaterthan__question_
  859. (Object firstArg, Object secondArg, Object[] restArgs) {
  860. ArgumentChecker.checkArraySize(restArgs, 0, "char>?");
  861. ArgumentChecker.checkArrayType(restArgs, characterClass, "char>?");
  862. return chainedTest(new ChainingTester() {
  863. public boolean test(Object n1, Object n2) {
  864. return (((Character) n1).charValue() >
  865. ((Character) n2).charValue());
  866. }
  867. },
  868. firstArg, secondArg, restArgs);
  869. }
  870. public static org.plt.types.Logic char_greaterthan__equal__question_
  871. (Object firstArg, Object secondArg, Object[] restArgs) {
  872. ArgumentChecker.checkArraySize(restArgs, 0, "char>=?");
  873. ArgumentChecker.checkArrayType(restArgs, characterClass, "char>=?");
  874. return chainedTest(new ChainingTester() {
  875. public boolean test(Object n1, Object n2) {
  876. return (((Character) n1).charValue() >=
  877. ((Character) n2).charValue());
  878. }
  879. },
  880. firstArg, secondArg, restArgs);
  881. }
  882. public static org.plt.types.Logic char_dash_upper_dash_case_question_(
  883. Object n) {
  884. ArgumentChecker.checkAtomType(n, "java.lang.Character",
  885. "char-upper-case?");
  886. return toLogic(Character.isUpperCase(((Character) n).charValue()));
  887. }
  888. public static org.plt.types.Logic char_dash_lower_dash_case_question_(
  889. Object n) {
  890. ArgumentChecker.checkAtomType(n, "java.lang.Character",
  891. "char-lower-case?");
  892. return toLogic(Character.isLowerCase(((Character) n).charValue()));
  893. }
  894. public static org.plt.types.Logic char_dash_numeric_question_(Object n) {
  895. ArgumentChecker
  896. .checkAtomType(n, "java.lang.Character", "char-numeric?");
  897. return toLogic(Character.isDigit(((Character) n).charValue()));
  898. }
  899. public static Object char_dash_upcase(Object n) {
  900. ArgumentChecker.checkAtomType(n, "java.lang.Character", "char-upcase");
  901. return new Character(Character.toUpperCase(((Character) n).charValue()));
  902. }
  903. public static Object char_dash_downcase(Object n) {
  904. ArgumentChecker.checkAtomType(n, "java.lang.Character", "char-downcase");
  905. return new Character(Character.toLowerCase(((Character) n).charValue()));
  906. }
  907. public static org.plt.types.Logic char_dash_whitespace_question_(Object n) {
  908. ArgumentChecker.checkAtomType(n, "java.lang.Character",
  909. "char-whitespace?");
  910. return toLogic(((Character) n).charValue() == ' ');
  911. }
  912. public static org.plt.types.Logic char_dash_alphabetic_question_(Object n) {
  913. ArgumentChecker.checkAtomType(n, "java.lang.Character",
  914. "char-alphabetic?");
  915. char v = ((Character) n).charValue();
  916. return toLogic((v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z'));
  917. }
  918. public static org.plt.types.Logic char_dash_ci_equal__question_
  919. (Object firstArg, Object secondArg, Object[] restArgs) {
  920. ArgumentChecker.checkArraySize(restArgs, 0, "char-ci=?");
  921. ArgumentChecker.checkArrayType(restArgs, characterClass, "char-ci=?");
  922. return chainedTest(new ChainingTester() {
  923. public boolean test(Object n1, Object n2) {
  924. return (Character.toLowerCase(((Character) n1).charValue())
  925. ==
  926. Character.toLowerCase(((Character) n2).charValue()));
  927. }
  928. },
  929. firstArg, secondArg, restArgs);
  930. }
  931. public static org.plt.types.Logic char_dash_ci_greaterthan__question_
  932. (Object firstArg, Object secondArg, Object[] restArgs) {
  933. ArgumentChecker.checkArraySize(restArgs, 0, "char-ci>?");
  934. ArgumentChecker.checkArrayType(restArgs, characterClass, "char-ci>?");
  935. return chainedTest(new ChainingTester() {
  936. public boolean test(Object n1, Object n2) {
  937. return (Character.toLowerCase(((Character) n1).charValue())
  938. >
  939. Character.toLowerCase(((Character) n2).charValue()));
  940. }
  941. },
  942. firstArg, secondArg, restArgs);
  943. }
  944. public static org.plt.types.Logic char_dash_ci_greaterthan__equal__question_
  945. (Object firstArg, Object secondArg, Object[] restArgs) {
  946. ArgumentChecker.checkArraySize(restArgs, 0, "char-ci>=?");
  947. ArgumentChecker.checkArrayType(restArgs, characterClass, "char-ci>=?");
  948. return chainedTest(new ChainingTester() {
  949. public boolean test(Object n1, Object n2) {
  950. return (Character.toLowerCase(((Character) n1).charValue())
  951. >=
  952. Character.toLowerCase(((Character) n2).charValue()));
  953. }
  954. },
  955. firstArg, secondArg, restArgs);
  956. }
  957. public static org.plt.types.Logic char_dash_ci_lessthan__question_
  958. (Object firstArg, Object secondArg, Object[] restArgs) {
  959. ArgumentChecker.checkArraySize(restArgs, 0, "char-ci<?");
  960. ArgumentChecker.checkArrayType(restArgs, characterClass, "char-ci<?");
  961. return chainedTest(new ChainingTester() {
  962. public boolean test(Object n1, Object n2) {
  963. return (Character.toLowerCase(((Character) n1).charValue())
  964. <
  965. Character.toLowerCase(((Character) n2).charValue()));
  966. }
  967. },
  968. firstArg, secondArg, restArgs);
  969. }
  970. public static org.plt.types.Logic char_dash_ci_lessthan__equal__question_
  971. (Object firstArg, Object secondArg, Object[] restArgs) {
  972. ArgumentChecker.checkArraySize(restArgs, 0, "char-ci<=?");
  973. ArgumentChecker.checkArrayType(restArgs, characterClass, "char-ci<=?");
  974. return chainedTest(new ChainingTester() {
  975. public boolean test(Object n1, Object n2) {
  976. return (Character.toLowerCase(((Character) n1).charValue())
  977. <=
  978. Character.toLowerCase(((Character) n2).charValue()));
  979. }
  980. },
  981. firstArg, secondArg, restArgs);
  982. }
  983. public static org.plt.types.Logic string_dash_ci_equal__question_
  984. (Object firstArg, Object secondArg, Object[] restArgs) {
  985. ArgumentChecker.checkArraySize(restArgs, 0, "string-ci=?");
  986. ArgumentChecker.checkArrayType(restArgs, stringClass, "string-ci=?");
  987. return chainedTest(new ChainingTester() {
  988. public boolean test(Object n1, Object n2) {
  989. return (((String) n1).toLowerCase().compareTo
  990. (((String) n2).toLowerCase()) == 0);
  991. }
  992. },
  993. firstArg, secondArg, restArgs);
  994. }
  995. public static org.plt.types.Logic string_dash_ci_greaterthan__question_
  996. (Object firstArg, Object secondArg, Object[] restArgs) {
  997. ArgumentChecker.checkArraySize(restArgs, 0, "string-ci>?");
  998. ArgumentChecker.checkArrayType(restArgs, stringClass, "string-ci>?");
  999. return chainedTest(new ChainingTester() {
  1000. public boolean test(Object n1, Object n2) {
  1001. return (((String) n1).toLowerCase().compareTo
  1002. (((String) n2).toLowerCase()) > 0);
  1003. }
  1004. },
  1005. firstArg, secondArg, restArgs);
  1006. }
  1007. public static org.plt.types.Logic string_dash_ci_greaterthan__equal__question_
  1008. (Object firstArg, Object secondArg, Object[] restArgs) {
  1009. ArgumentChecker.checkArraySize(restArgs, 0, "string-ci>=?");
  1010. ArgumentChecker.checkArrayType(restArgs, stringClass, "string-ci>=?");
  1011. return chainedTest(new ChainingTester() {
  1012. public boolean test(Object n1, Object n2) {
  1013. return (((String) n1).toLowerCase().compareTo
  1014. (((String) n2).toLowerCase()) >= 0);
  1015. }
  1016. },
  1017. firstArg, secondArg, restArgs);
  1018. }
  1019. public static org.plt.types.Logic string_dash_ci_lessthan__question_
  1020. (Object firstArg, Object secondArg, Object[] restArgs) {
  1021. ArgumentChecker.checkArraySize(restArgs, 0, "string-ci<?");
  1022. ArgumentChecker.checkArrayType(restArgs, stringClass, "string-ci<?");
  1023. return chainedTest(new ChainingTester() {
  1024. public boolean test(Object n1, Object n2) {
  1025. return (((String) n1).toLowerCase().compareTo
  1026. (((String) n2).toLowerCase()) < 0);
  1027. }
  1028. },
  1029. firstArg, secondArg, restArgs);
  1030. }
  1031. public static org.plt.types.Logic string_dash_ci_lessthan__equal__question_
  1032. (Object firstArg, Object secondArg, Object[] restArgs) {
  1033. ArgumentChecker.checkArraySize(restArgs, 0, "string-ci<=?");
  1034. ArgumentChecker.checkArrayType(restArgs, stringClass, "string-ci<=?");
  1035. return chainedTest(new ChainingTester() {
  1036. public boolean test(Object n1, Object n2) {
  1037. return (((String) n1).toLowerCase().compareTo
  1038. (((String) n2).toLowerCase()) <= 0);
  1039. }
  1040. },
  1041. firstArg, secondArg, restArgs);
  1042. }
  1043. public static org.plt.types.Symbol string_dash__greaterthan_symbol(Object n) {
  1044. ArgumentChecker.checkAtomType(n, "java.lang.String", "string->symbol");
  1045. return Symbol.makeInstance((String) n);
  1046. }
  1047. public static org.plt.types.List string_dash__greaterthan_list(Object n) {
  1048. ArgumentChecker.checkAtomType(n, "java.lang.String", "string->list");
  1049. org.plt.types.List ret = Empty.EMPTY;
  1050. for (int i = ((String) n).length() - 1; i >= 0; i--)
  1051. ret = new Pair(new Character(((String) n).charAt(i)), ret);
  1052. return ret;
  1053. }
  1054. public static Object string_dash_append(Object firstArg, Object[] arr) {
  1055. ArgumentChecker.checkArrayType(arr, stringClass, "string-append");
  1056. StringBuffer buf = new StringBuffer();
  1057. buf.append(firstArg.toString());
  1058. for (int i = 0; i < arr.length; i++) {
  1059. buf.append(arr[i].toString());
  1060. }
  1061. return buf.toString();
  1062. }
  1063. public static Object string(Object firstArg, Object[] arr) {
  1064. ArgumentChecker.checkArrayType(arr, characterClass, "string");
  1065. char[] ret = new char[arr.length + 1];
  1066. ret[0] = ((Character) firstArg).charValue();
  1067. for (int i = 0; i < arr.length; i++)
  1068. ret[i+1] = ((Character) arr[i]).charValue();
  1069. return new String(ret);
  1070. }
  1071. public static Object make_dash_string(Object n1, Object n2) {
  1072. if (natural_question_(n1).isFalse())
  1073. throw new SchemeException(
  1074. "make-string: expects type <non-negative exact integer> as 1st argument, given: "
  1075. + n1 + "; other arguments were: " + n2);
  1076. if (char_question_(n2).isFalse())
  1077. throw new SchemeException(
  1078. "make-string: expects type <character> as 2nd argument, given: "
  1079. + n2 + "; other arguments were: " + n1);
  1080. char[] ret = new char[((org.plt.types.Number) n1).toInt()];
  1081. for (int i = 0; i < ret.length; i++)
  1082. ret[i] = ((Character) n2).charValue();
  1083. return new String(ret);
  1084. }
  1085. public static Object list_dash__greaterthan_string(Object n) {
  1086. ArgumentChecker.checkListType(n, characterClass, "list->string");
  1087. char[] ret = new char[Kernel.length(n).toInt()];
  1088. for (int i = 0; i < ret.length; i++) {
  1089. ret[i] = ((Character) ((org.plt.types.List) n).first()).charValue();
  1090. n = ((org.plt.types.List) n).rest();
  1091. }
  1092. return new String(ret);
  1093. }
  1094. public static org.plt.types.Number char_dash__greaterthan_integer(Object n) {
  1095. ArgumentChecker
  1096. .checkAtomType(n, "java.lang.Character", "char->integer");
  1097. return new Rational((int) ((Character) n).charValue(), 1);
  1098. }
  1099. public static Object integer_dash__greaterthan_char(Object n) {
  1100. // ArgumentCheck.checkAtomType(n, "org.plt.types.Number")
  1101. ArgumentChecker.checkAtomType(n, new PropertyChecker() {
  1102. public boolean satisfied(Object n) {
  1103. return (n instanceof org.plt.types.Number)
  1104. && ((org.plt.types.Number) n).isInteger();
  1105. }
  1106. }, "exact integer", "integer->char");
  1107. return new Character((char) (((org.plt.types.Number) n).toInt()));
  1108. }
  1109. public static org.plt.types.Logic member(Object n1, Object n2) {
  1110. if ((n2 instanceof org.plt.types.List) == false)
  1111. throw new SchemeException(
  1112. "member: second argument must be of type <list>, given: "
  1113. + n1 + " and " + n2);
  1114. while (((org.plt.types.List) n2).isEmpty() == false
  1115. && Kernel
  1116. .equal_question_(n1, ((org.plt.types.List) n2).first())
  1117. .isFalse())
  1118. n2 = ((org.plt.types.List) n2).rest();
  1119. return toLogic(((org.plt.types.List) n2).isEmpty() == false);
  1120. }
  1121. public static Object memq(Object n1, Object n2) {
  1122. if ((n2 instanceof org.plt.types.List) == false)
  1123. throw new SchemeException(
  1124. "memq: second argument must be of type <list>, given: "
  1125. + n1 + " and " + n2);
  1126. while (((org.plt.types.List) n2).isEmpty() == false
  1127. && Kernel.eq_question_(n1, ((org.plt.types.List) n2).first())
  1128. .isFalse())
  1129. n2 = ((org.plt.types.List) n2).rest();
  1130. if (((org.plt.types.List) n2).isEmpty() == false)
  1131. return n2;
  1132. else
  1133. return Logic.FALSE;
  1134. }
  1135. public static Object memv(Object n1, Object n2) {
  1136. if ((n2 instanceof org.plt.types.List) == false)
  1137. throw new SchemeException(
  1138. "memv: second argument must be of type <list>, given: "
  1139. + n1 + " and " + n2);
  1140. while (((org.plt.types.List) n2).isEmpty() == false
  1141. && Kernel.eqv_question_(n1, ((org.plt.types.List) n2).first())
  1142. .isFalse())
  1143. n2 = ((org.plt.types.List) n2).rest();
  1144. if (((org.plt.types.List) n2).isEmpty() == false)
  1145. return n2;
  1146. else
  1147. return Logic.FALSE;
  1148. }
  1149. public static org.plt.types.List list(Object[] args) {
  1150. org.plt.types.List result = Empty.EMPTY;
  1151. for(int i = 0; i < args.length; i++) {
  1152. result = cons(args[i], result);
  1153. }
  1154. return reverse(result);
  1155. }
  1156. public static org.plt.types.List list_star_(Object firstArg, Object[] args) {
  1157. org.plt.types.List result = Empty.EMPTY;
  1158. if (args.length == 0)
  1159. return (org.plt.types.List) firstArg;
  1160. result = cons(firstArg, result);
  1161. for(int i = 0; i < args.length-1; i++) {
  1162. result = cons(args[i], result);
  1163. }
  1164. return append2(reverse(result),
  1165. (org.plt.types.List) args[args.length -1]);
  1166. }
  1167. public static org.plt.types.List remove(Object _aList,
  1168. Object anElt) {
  1169. org.plt.types.List aList = (org.plt.types.List) _aList;
  1170. org.plt.types.List soFar = Empty.EMPTY;
  1171. while (!aList.isEmpty()) {
  1172. if (aList.first().equals(anElt)) {
  1173. return append2(reverse(soFar),
  1174. aList.rest());
  1175. } else {
  1176. soFar = cons(aList.first(), soFar);
  1177. aList = aList.rest();
  1178. }
  1179. }
  1180. return (org.plt.types.List) _aList;
  1181. }
  1182. public static org.plt.types.List append(Object firstArg, Object[] args) {
  1183. org.plt.types.List result = (org.plt.types.List) firstArg;
  1184. for(int i = 0; i < args.length; i++) {
  1185. result = append2(result, args[i]);
  1186. }
  1187. return result;
  1188. }
  1189. private static org.plt.types.List append2(Object n1, Object n2) {
  1190. Object[] args = { n1, n2 };
  1191. ArgumentChecker.checkArrayType(args, listClass, "append");
  1192. org.plt.types.List rev = reverse(n1);
  1193. while (!rev.isEmpty()) {
  1194. n2 = cons(rev.first(), n2);
  1195. rev = rev.rest();
  1196. }
  1197. return (org.plt.types.List) n2;
  1198. }
  1199. public static Object assq(Object n1, Object n2) {
  1200. ArgumentChecker.checkListType(n2, pairClass, "assq", 2);
  1201. while (((org.plt.types.List) n2).isEmpty() == false
  1202. && Kernel.eq_question_(
  1203. n1,
  1204. (((org.plt.types.Pair) ((org.plt.types.List) n2)
  1205. .first())).first()).isFalse())
  1206. n2 = ((org.plt.types.List) n2).rest();
  1207. if (((org.plt.types.List) n2).isEmpty() == false)
  1208. return ((org.plt.types.Pair) n2).first();
  1209. else
  1210. return Logic.FALSE;
  1211. }
  1212. public static org.plt.types.Number current_dash_seconds() {
  1213. return new Rational(
  1214. (int) (Calendar.getInstance().getTime().getTime() / 1000), 1);
  1215. }
  1216. public static org.plt.types.Logic complex_question_(Object n) {
  1217. return number_question_(n);
  1218. }
  1219. public static org.plt.types.Number real_dash_part(Object n) {
  1220. ArgumentChecker.checkAtomType(n, "org.plt.types.Number", "real_part");
  1221. if (n instanceof Complex)
  1222. return ((Complex) n).getRealPart();
  1223. else
  1224. return (org.plt.types.Number) n;
  1225. }
  1226. public static org.plt.types.Number imag_dash_part(Object n) {
  1227. ArgumentChecker.checkAtomType(n, "org.plt.types.Number", "imag-part");
  1228. if (n instanceof Complex)
  1229. return ((Complex) n).getImgPart();
  1230. else
  1231. return FloatPoint.ZERO;
  1232. }
  1233. public static void exit() {
  1234. System.exit(0);
  1235. }
  1236. public static org.plt.types.Logic equal_tilde__question_(Object n1,
  1237. Object n2, Object n3) {
  1238. ArgumentChecker.checkAtomType(n3, new PropertyChecker() {
  1239. public boolean satisfied(Object n) {
  1240. return (real_question_(n).isTrue() && negative_question_(n)
  1241. .isFalse());
  1242. }
  1243. }, "non-negative-real", "equal~?", 3);
  1244. if (real_question_(n1).isTrue() && real_question_(n2).isTrue())
  1245. return _equal__tilde_(n1, n2, n3);
  1246. return equal_question_(n1, n2);
  1247. }
  1248. public static org.plt.types.Logic exact_question_(Object n) {
  1249. ArgumentChecker.checkAtomType(n, "org.plt.types.Number", "exact?");
  1250. if (rational_question_(n).isTrue())
  1251. return Logic.TRUE;
  1252. if (n instanceof Complex
  1253. && ((Complex) n).getRealPart() instanceof Rational)
  1254. return Logic.TRUE;
  1255. if (n instanceof Complex
  1256. && ((Complex) n).getRealPart() instanceof Rational
  1257. && ((Complex) n).getImgPart() instanceof Rational)
  1258. return Logic.TRUE;
  1259. return Logic.FALSE;
  1260. }
  1261. public static org.plt.types.Logic inexact_question_(Object n) {
  1262. ArgumentChecker.checkAtomType(n, "org.plt.types.Number", "inexact?");
  1263. return exact_question_(n).negate();
  1264. }
  1265. public static org.plt.types.Number exact_dash__greaterthan_inexact(Object n) {
  1266. ArgumentChecker.checkAtomType(n, "org.plt.types.Number",
  1267. "exact->inexact");
  1268. if (inexact_question_(n).isTrue())
  1269. return (org.plt.types.Number) n;
  1270. else
  1271. return ((Rational) n).toFloatPoint();
  1272. }
  1273. public static org.plt.types.Number inexact_dash__greaterthan_exact(Object n) {
  1274. ArgumentChecker.checkAtomType(n, "org.plt.types.Number",
  1275. "inexact->exact");
  1276. if (exact_question_(n).isTrue())
  1277. return (org.plt.types.Number) n;
  1278. else
  1279. return new Rational(((FloatPoint) n).toInt(), 1);
  1280. }
  1281. public static org.plt.types.Number angle(Object n) {
  1282. ArgumentChecker.checkAtomType(n, "org.plt.types.Number", "angle");
  1283. return ((org.plt.types.Number) n).angle();
  1284. }
  1285. public static org.plt.types.Number conjugate(Object n) {
  1286. ArgumentChecker.checkAtomType(n, "org.plt.types.Number", "conjugate");
  1287. return ((org.plt.types.Number) n).conjugate();
  1288. }
  1289. public static org.plt.types.Number magnitude(Object n) {
  1290. ArgumentChecker.checkAtomType(n, "org.plt.types.Number", "magnitude");
  1291. return ((org.plt.types.Number) n).magnitude();
  1292. }
  1293. public static org.plt.types.Logic eof_dash_object_question_(Object n) {
  1294. if (n instanceof EofObject) {
  1295. return Logic.TRUE;
  1296. } else {
  1297. return Logic.FALSE;
  1298. }
  1299. }
  1300. }