vect.red 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. MODULE VECT; % Vector support routines.
  2. % Authors: Mary Ann Moore and Arthur C. Norman.
  3. % Modified by: James H. Davenport.
  4. EXPORTS MKUNIQUEVECT,MKVEC,MKVECF2Q,MKIDENM,COPYVEC,VECSORT,SWAP,
  5. NON!-NULL!-VEC,MKVECT2;
  6. SYMBOLIC PROCEDURE MKUNIQUEVECT V;
  7. BEGIN SCALAR U,N;
  8. N:=UPBV V;
  9. FOR I:=0:N DO BEGIN
  10. SCALAR UU;
  11. UU:=GETV(V,I);
  12. IF NOT (UU MEMBER U)
  13. THEN U:=UU.U
  14. END;
  15. RETURN MKVEC U
  16. END;
  17. SYMBOLIC PROCEDURE MKVEC(L);
  18. BEGIN SCALAR V,I;
  19. V:=MKVECT(ISUB1 LENGTH L);
  20. I:=0;
  21. WHILE L DO <<PUTV(V,I,CAR L); I:=IADD1 I; L:=CDR L>>;
  22. RETURN V
  23. END;
  24. SYMBOLIC PROCEDURE MKVECF2Q(L);
  25. BEGIN
  26. SCALAR V,I,LL;
  27. V:=MKVECT(ISUB1 LENGTH L);
  28. I:=0;
  29. WHILE L DO <<
  30. LL:=CAR L;
  31. IF LL = 0 THEN LL:=NIL;
  32. PUTV(V,I,!*F2Q LL);
  33. I:=IADD1 I;
  34. L:=CDR L >>;
  35. RETURN V
  36. END;
  37. SYMBOLIC PROCEDURE MKIDENM N;
  38. BEGIN
  39. SCALAR ANS,U;
  40. SCALAR C0,C1;
  41. C0:=NIL ./ 1;
  42. C1:= 1 ./ 1;
  43. % constants.
  44. ANS:=MKVECT(N);
  45. FOR I:=0 STEP 1 UNTIL N DO <<
  46. U:=MKVECT N;
  47. FOR J:=0 STEP 1 UNTIL N DO
  48. IF I IEQUAL J
  49. THEN PUTV(U,J,C1)
  50. ELSE PUTV(U,J,C0);
  51. PUTV(ANS,I,U) >>;
  52. RETURN ANS
  53. END;
  54. SYMBOLIC PROCEDURE COPYVEC(V,N);
  55. BEGIN SCALAR NEW;
  56. NEW:=MKVECT(N);
  57. FOR I:=0:N DO PUTV(NEW,I,GETV(V,I));
  58. RETURN NEW
  59. END;
  60. SYMBOLIC PROCEDURE VECSORT(U,L);
  61. % Sorts vector v of numbers into decreasing order.
  62. % Performs same interchanges of all vectors in the list l.
  63. BEGIN
  64. SCALAR J,K,N,V,W;
  65. N:=UPBV U;% elements 0...n exist.
  66. % algorithm used is a bubble sort.
  67. FOR I:=1:N DO BEGIN
  68. V:=GETV(U,I);
  69. K:=I;
  70. LOOP:
  71. J:=K;
  72. K:=ISUB1 K;
  73. W:=GETV(U,K);
  74. IF V<=W
  75. THEN GOTO ORDERED;
  76. PUTV(U,K,V);
  77. PUTV(U,J,W);
  78. MAPC(L,FUNCTION (LAMBDA U;SWAP(U,J,K)));
  79. IF K>0
  80. THEN GOTO LOOP;
  81. ORDERED:
  82. END;
  83. RETURN NIL
  84. END;
  85. SYMBOLIC PROCEDURE SWAP(U,J,K);
  86. IF NULL U
  87. THEN NIL
  88. ELSE BEGIN
  89. SCALAR V;
  90. %swaps elements i,j of vector u.
  91. V:=GETV(U,J);
  92. PUTV(U,J,GETV(U,K));
  93. PUTV(U,K,V)
  94. END;
  95. SYMBOLIC PROCEDURE NON!-NULL!-VEC V;
  96. BEGIN
  97. SCALAR CNT;
  98. CNT := 0;
  99. FOR I:=0:UPBV V DO
  100. IF GETV(V,I)
  101. THEN CNT:=IADD1 CNT;
  102. RETURN CNT
  103. END;
  104. SYMBOLIC PROCEDURE MKVECT2(N,INITIAL);
  105. BEGIN
  106. SCALAR U;
  107. U:=MKVECT N;
  108. FOR I:=0:N DO
  109. PUTV(U,I,INITIAL);
  110. RETURN U
  111. END;
  112. ENDMODULE;
  113. END;