general.red 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. module general; % General functions for the support of REDUCE.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1999 Anthony C. Hearn. All rights reserved.
  4. global '(!!arbint);
  5. !!arbint := 0; % Index for arbitrary constants.
  6. symbolic procedure atomlis u;
  7. null u or (atom car u and atomlis cdr u);
  8. symbolic procedure carx(u,v);
  9. if null cdr u then car u
  10. else rerror(alg,5,list("Wrong number of arguments to",v));
  11. % We assume concat2 is defined in the underlying Lisp system.
  12. % symbolic macro procedure concat u;
  13. % if null u then nil else expand(cdr u,'concat2);
  14. % symbolic procedure delasc(u,v);
  15. % if null v then nil
  16. % else if atom car v or u neq caar v then car v . delasc(u,cdr v)
  17. % else cdr v;
  18. % This definition, due to A.C. Norman, avoids recursion.
  19. symbolic procedure delasc(u,v);
  20. begin scalar w;
  21. while v do
  22. <<if atom car v or u neq caar v then w := car v . w; v := cdr v>>;
  23. return reversip w
  24. end;
  25. symbolic procedure eqexpr u;
  26. % Returns true if U is an equation or similar structure
  27. % (e.g., a rule).
  28. not atom u
  29. and flagp(car u,'equalopr) and cddr u and null cdddr u;
  30. flag('(eq equal),'equalopr);
  31. symbolic procedure evenp x; remainder(x,2)=0;
  32. flag('(evenp),'opfn); % Make a symbolic operator.
  33. symbolic procedure lengthc u;
  34. %gives character length of U excluding string and escape chars;
  35. begin integer n; scalar x;
  36. n := 0;
  37. x := explode u;
  38. if car x eq '!" then return length x-2;
  39. while x do
  40. <<if car x eq '!! then x := cdr x;
  41. n := n+1;
  42. x := cdr x>>;
  43. return n
  44. end;
  45. symbolic procedure makearbcomplex;
  46. begin scalar ans;
  47. !!arbint := !!arbint+1;
  48. ans := car(simp!*(list('arbcomplex, !!arbint)));
  49. % This CAR is NUMR, which is not yet defined.
  50. return ans
  51. end;
  52. symbolic procedure mapcons(u,v);
  53. for each j in u collect v . j;
  54. symbolic procedure mappend(u,v);
  55. for each j in u collect append(v,j);
  56. symbolic procedure nlist(u,n);
  57. if n=0 then nil else u . nlist(u,n-1);
  58. symbolic procedure nth(u,n);
  59. car pnth(u,n);
  60. symbolic procedure pnth(u,n);
  61. if null u then rerror(alg,6,"Index out of range")
  62. else if n=1 then u
  63. else pnth(cdr u,n-1);
  64. symbolic procedure permp(u,v);
  65. % This used to use EQ. However, SUBST use requires =.
  66. if null u then t
  67. else if car u=car v then permp(cdr u,cdr v)
  68. else not permp(cdr u,subst(car v,car u,cdr v));
  69. symbolic procedure permutations u;
  70. % Returns list of all permutations of the list u.
  71. if null u then list u
  72. else for each j in u join mapcons(permutations delete(j,u),j);
  73. symbolic procedure posintegerp u;
  74. % True if U is a positive (non-zero) integer.
  75. fixp u and u>0;
  76. symbolic procedure remove(x,n);
  77. % Returns X with Nth element removed;
  78. if null x then nil
  79. else if n=1 then cdr x
  80. else car x . remove(cdr x,n-1);
  81. symbolic procedure repasc(u,v,w);
  82. % Replaces value of key U by V in association list W.
  83. if null w then rerror(alg,7,list("key",u,"not found"))
  84. else if u = caar w then (u . v) . cdr w
  85. else car w . repasc(u,v,cdr w);
  86. symbolic procedure repeats x;
  87. if null x then nil
  88. else if car x member cdr x then car x . repeats cdr x
  89. else repeats cdr x;
  90. symbolic procedure revpr u;
  91. cdr u . car u;
  92. symbolic procedure smember(u,v);
  93. %determines if S-expression U is a member of V at any level;
  94. if u=v then t
  95. else if atom v then nil
  96. else smember(u,car v) or smember(u,cdr v);
  97. symbolic procedure smemql(u,v);
  98. %Returns those members of id list U contained in V at any
  99. %level (excluding quoted expressions);
  100. if null u then nil
  101. else if smemq(car u,v) then car u . smemql(cdr u,v)
  102. else smemql(cdr u,v);
  103. symbolic procedure smemqlp(u,v);
  104. %True if any member of id list U is contained at any level
  105. %in V (exclusive of quoted expressions);
  106. if null v or numberp v then nil
  107. else if atom v then v memq u
  108. else if car v eq 'quote then nil
  109. else smemqlp(u,car v) or smemqlp(u,cdr v);
  110. symbolic procedure spaces n; for i := 1:n do prin2 " ";
  111. symbolic procedure subla(u,v);
  112. % Substitutes the atom u in v. Retains previous structure where
  113. % possible.
  114. if null u or null v then v
  115. else if atom v then (if x then cdr x else v) where x=atsoc(v,u)
  116. else (if y=v then v else y) where y=subla(u,car v) . subla(u,cdr v);
  117. symbolic procedure xnp(u,v);
  118. %returns true if the atom lists U and V have at least one common
  119. %element;
  120. u and (car u memq v or xnp(cdr u,v));
  121. endmodule;
  122. end;