lto.red 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. % ----------------------------------------------------------------------
  2. % $Id: lto.red,v 1.6 1999/03/24 12:29:57 dolzmann Exp $
  3. % ----------------------------------------------------------------------
  4. % Copyright (c) 1995-1999 Andreas Dolzmann and Thomas Sturm
  5. % ----------------------------------------------------------------------
  6. % $Log: lto.red,v $
  7. % Revision 1.6 1999/03/24 12:29:57 dolzmann
  8. % Added the procedure lto_max for computing the maximum of a list of
  9. % integers.
  10. %
  11. % Revision 1.5 1999/03/22 15:26:15 dolzmann
  12. % Changed copyright information.
  13. % Added and reformatted comments.
  14. %
  15. % Revision 1.4 1997/11/05 06:35:10 dolzmann
  16. % Added comments.
  17. % Moved system dependent procedures to the end of the file.
  18. % Updated copyright message.
  19. % Replaced "written by" in the CVS header by the usual copyright message.
  20. %
  21. % Revision 1.3 1996/10/17 12:31:52 sturm
  22. % Moved sconcat2, sconcat, and at2str from qepcad.red to lto.red.
  23. %
  24. % Revision 1.2 1996/09/05 11:17:36 dolzmann
  25. % Added procedures delq, delqip, delqip1, and adjoin for non-PSL versions.
  26. %
  27. % Revision 1.1 1996/04/30 12:06:44 sturm
  28. % Merged ioto, lto, and sfto into rltools.
  29. %
  30. % Revision 1.1 1996/03/22 12:11:09 sturm
  31. % Moved.
  32. %
  33. % Revision 1.4 1996/02/18 13:52:15 sturm
  34. % Added procedure lto_natsoc.
  35. %
  36. % Revision 1.3 1996/02/18 12:39:18 dolzmann
  37. % Added procedure lto_cassoc.
  38. %
  39. % Revision 1.2 1995/06/21 07:35:47 sturm
  40. % Added procedures lto_nconcn, lto_alunion, and lto_almerge.
  41. %
  42. % Revision 1.1 1995/05/29 14:47:19 sturm
  43. % Initial check-in.
  44. %
  45. % ----------------------------------------------------------------------
  46. lisp <<
  47. fluid '(lto_rcsid!* lto_copyright!*);
  48. lto_rcsid!* := "$Id: lto.red,v 1.6 1999/03/24 12:29:57 dolzmann Exp $";
  49. lto_copyright!* := "Copyright (c) 1995-1999 by A. Dolzmann and T. Sturm"
  50. >>;
  51. module lto;
  52. % List tools.
  53. procedure lto_insert(x,l);
  54. % List tools insert. [x] is any S-expression, [l] is a list. Conses
  55. % [x] to [l] if [x] is not already member of [l].
  56. if x member l then l else x . l;
  57. procedure lto_insertq(x,l);
  58. % List tools insert testing with memq. [x] is any S-expression, [l]
  59. % is a list. Conses [x] to [l] if [x] is not already [memq].
  60. if x memq l then l else x . l;
  61. procedure lto_mergesort(l,sortp);
  62. % List tools merge sort. [l] is a list; [sortp] is a function that
  63. % implements an ordering. Returns a list. [l] is sorted such that
  64. % [sortp] holds between each two adjacent elements.
  65. begin scalar crit,s1,s2;
  66. % Empty and one-element lists are already sorted.
  67. if null l or null cdr l then return l;
  68. % Construct two sets by comparing all others with the first one.
  69. crit := car l;
  70. for each entry in cdr l do
  71. if apply(sortp,{entry,crit}) then
  72. s1 := entry . s1
  73. else
  74. s2 := entry . s2;
  75. % sort the two lists recursively and place crit in between
  76. return nconc(lto_mergesort(s1,sortp),crit . lto_mergesort(s2,sortp))
  77. end;
  78. procedure lto_catsoc(key,al);
  79. % List tools conditional atsoc. [key] is an identifier; [al] is an
  80. % alist $((k_1 . e_1),...,(k_n . e_n))$. Returns $e_i$ if [key] is
  81. % [eq] to $k_i$, [nil] else.
  82. (if x then cdr x) where x=atsoc(key,al);
  83. procedure lto_natsoc(key,al);
  84. % List tools conditional number atsoc. [key] is an identifier; [al]
  85. % is an alist $((k_1 . e_1),...,(k_n . e_n))$. Returns $e_i$ if
  86. % $[key]=k_i$, 0 else.
  87. (if w then cdr w else 0) where w=atsoc(key,al);
  88. procedure lto_cassoc(key,al);
  89. % List tools conditional assoc. [key] is an identifier; [al] is an
  90. % alist $((k_1 . e_1),...,(k_n . e_n))$. Returns $e_i$ if
  91. % $[key]=k_i$, [nil] else.
  92. (if x then cdr x) where x=assoc(key,al);
  93. procedure lto_nconcn(l);
  94. % List tools non-constructive concatenate n-ary. [l] is a list of
  95. % lists. Returns a list. The returned list is the concatenation of
  96. % all lists in [l]. The lists in [l] are possibly modyfied.
  97. if cdr l then nconc(car l,lto_nconcn cdr l) else car l;
  98. procedure lto_alunion(all);
  99. % List tools assoc list union. [all] is a list of alists $((k1 .
  100. % e1) ... (kn . en))$, where all ki are unique and all ei are
  101. % lists. Merges all alists in [all] into one alist, where the keys
  102. % are the union of all ki appearing in the members of [all], and
  103. % the entry to each key is the union of the lists that are entries
  104. % to the key within the members of [all]. All members of [all] are
  105. % modified by this function.
  106. lto_almerge(all,'union);
  107. procedure lto_almerge(all,merge);
  108. % List tools assoc list merge. [all] is a list of alists $((k1 .
  109. % e1) ... (kn . en))$, where all ki are unique and all ei are
  110. % lists; [merge] is a function that maps two lists to another list.
  111. % Merges all alists in [all] into one alist, where the keys are the
  112. % union of all ki appearing in the members of [all], and the entry
  113. % to each key is obtained from the entries in [all] by applying
  114. % [merge]. All members of [all] are modified by this function.
  115. begin scalar l2,a;
  116. if null all then return nil;
  117. if null cdr all then return car all;
  118. if null cddr all then <<
  119. l2 := cadr all;
  120. for each pair in car all do <<
  121. a := assoc(car pair,l2);
  122. if a then
  123. cdr a := apply(merge,{cdr pair,cdr a})
  124. else
  125. l2 := pair . l2
  126. >>;
  127. return l2
  128. >>;
  129. return lto_almerge({car all,lto_almerge(cdr all,merge)},merge)
  130. end;
  131. procedure lto_sconcat2(s1,s2);
  132. % List tools string concatenation 2. [s1] and [s2] are strings.
  133. % Returns a string. The returned string is the concatenation
  134. % [s1][s2].
  135. compress append(reversip cdr reversip explode s1,cdr explode s2);
  136. procedure lto_sconcat(l);
  137. % List tools string concatenation. [l] is a list of strings.
  138. % Returns a string. The returned string is the concatenation of all
  139. % strings in [l].
  140. if l then
  141. if cdr l then
  142. lto_sconcat2(car l,lto_sconcat cdr l)
  143. else
  144. car l;
  145. procedure lto_at2str(s);
  146. % List tools atom to string. [s] is an atom. Returns the print name
  147. % of the atom [s] as a string.
  148. compress('!" . reversip('!" . reversip explode s));
  149. procedure lto_max(l);
  150. % List tools maximum of a list. [l] is a list of integers. Rerurns
  151. % the maximum of [l].
  152. if null cdr l then car l else max(car l,lto_max cdr l);
  153. !#if (not (memq 'psl lispsystem!*))
  154. procedure delq(x,l);
  155. % Delete with memq. [x] is ANY; [l] is a list. Returns a list.
  156. % The first occurence of an element identical to [x] in [l] is
  157. % deleted.
  158. if l then if car l eq x then cdr l else car l . delq(x,cdr l);
  159. !#endif
  160. !#if (not (memq 'psl lispsystem!*))
  161. procedure delqip(u,v);
  162. % Delete with memq in place. [u] is ANY; [v] is a list. Returns
  163. % a list. The first occurence of an element identical to [u] in
  164. % [v] is deleted [v] is possibly modified.
  165. if not pairp v then
  166. v
  167. else if u eq car v then
  168. cdr v
  169. else <<
  170. delqip1(u,v);
  171. v
  172. >>;
  173. !#endif
  174. !#if (not (memq 'psl lispsystem!*))
  175. procedure delqip1(u,v);
  176. % Delete with memq in place subroutine. [u] is ANY; [v] is a
  177. % list, such that [not(car v eq u)]. Returns a list. The first
  178. % occurence of an element identical to [u] in [v] is deleted [v]
  179. % is possibly modified.
  180. if not pairp cdr v then
  181. nil
  182. else if u eq cadr v then
  183. rplacd(v,cddr v)
  184. else
  185. delqip1(u,cdr v);
  186. !#endif
  187. !#if (not (memq 'psl lispsystem!*))
  188. procedure adjoin(x,l);
  189. % Adjoin. [x] is any S-expression, [l] is a list. Conses [x] to
  190. % [l] if [x] is not already member of [l].
  191. if x member l then l else x . l;
  192. !#endif
  193. endmodule; % [lto]
  194. end; % of file