summaryrefslogtreecommitdiff
path: root/ipl/procs/pdco.icn
blob: cd239c17bf5161a92f97628ee8f2fe3916c3f370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
############################################################################
#
#	File:     pdco.icn
#
#	Subject:  Procedures for programmer-defined control operations
#
#	Authors:  Ralph E. Griswold and Robert J. Alexander
#
#	Date:     March 4, 2003
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#  
#  These procedures use co-expressions to used to model the built-in
#  control structures of Icon and also provide new ones.
#
#   AddTabbyPDCO{e, i}	adds tabby to treadling sequence
#  
#   AllparAER{e1,e2, ...}
#			parallel evaluation with last result
#			used for short sequences
#
#   AltPDCO{e1,e2}	models e1 | e2
#
#   BinopPDCO{op,e1,e2} produces the result of applying op to e1 and e2
#
#   CFapproxPDCO{e}	produce sequence of approximations for the
#			continued-fraction sequence e
#  
#   ComparePDCO{e1,e2}	compares result sequences of e1 and e2
#  
#   ComplintPDCO{e}	produces the integers not in e
#
#   CondPDCO{e1,e2, ...}
#			models the generalized Lisp conditional
#
#   CumsumPDCO{e}	generates the cumulative sum of the terms of e
#  
#   CycleparAER{e1,e2, ...}
#			parallel evaluation with shorter sequences
#			re-evaluated
#
#   DecimatePDCO{e1, e2}
#			"decimate" e1 by deleting e2-numbered terms
#			(e2 is assumed to be an increasing sequence).
#
#   DecimationPDCO{e}	produce a decimation sequence from e1 by
#			deleting even-valued terms and replacing
#			odd-valued terms by their position.
#
#   DecollatePDCO{e, i}	decollate e according to parity of i
#
#   DeltaPDCO{e1}	produces the difference of the values in e1
#
#   ElevatePDCO{e1, m, n}
#			elevate e1 mod n to n values
#  
#   EveryPDCO{e1,e2}	models every e1 do e2
#
#   ExtendSeqPDCO{e1,i}	extends e1 to i results
#  
#   ExtractAER{e1,e2, ...}
#			extract results of even-numbered arguments
#			according to odd-numbered values
#
#   FifoAER{e1,e2, ...}	reversal of lifo evaluation
#
#   FriendlyPDCO{m, k, e3}
#			friendly sequence starting at k shaft mod m
#
#   GaltPDCO{e1,e2, ...}
#			produces the results of concatenating the
#   		   	sequences for e1, e2, ...
#
#   GconjPDCO{e1,e2,...}
#			models generalized conjunction: e1 & e2 & ...
#
#   The programmer-defined control operation above shows an interesting
#   technique for modeling conjunction via recursive generative
#   procedures.
#
#   HistoPDCO{e,i}	generates histogram for e limited to i terms;
#			default 100.
#
#   IncreasingPDCO{e}	filters out non-increasing values in integer
#			sequence
#
#   IndexPDCO{e1,e2}	produce e2-th terms from e1
#
#   InterPDCO{e1,e2, ...}
#			produces results of e1, e2, ... alternately
#  
#   LcondPDCO{e1,e2, ...}
#			models the Lisp conditional
#
#   LengthPDCO{e}	returns the length of e
#  
#   LifoAER{e1,e2, ...}	models standard Icon "lifo" evaluation
#
#   LimitPDCO{e1,e2}	models e1 \ e2
#
#   ListPDCO{e,i}	produces a list of the first i results from e
#
#   LowerTrimPDCO{e}	lower trim
#
#   MapPDCO{e1,e2}	maps values of e1 in the order they first appear
#			to values of e2 (as needed)
#
#   OddEven{e}		forces odd/even sequence
#
#   PalinPDCO{e}	x produces results of concatenating the
#			sequences for e and then its reverse.
#
#   ParallelPDCO{e1,e2, ...}
#			synonym for InterPDCO{e1, e2, ...}
#  
#   ParallelAER{e1,e2, ...}
#			parallel evaluation terminating on
#			shortest sequence
#
#   PatternPalinPDCO{e, i}
#			produces pattern palindrome.  If i is given,
#			e is truncated to length i.
#
#   PeriodPDCO{e, i}	generates the periodic part of e; i values are
#			used to find the period
#
#   PermutePDCO{e1,e2}	permutes each n-subsequence of e1 by the
#   		  	n positional values in lists from e2.  If a list does
#			not consist of all the integers in the range 1 to
#   		  	n, "interesting" things happen (see the use
#   		   	of map() for transpositions).
#
#   PivotPDCO{e, m}	produces pivot points from e % m; m default 100
#
#   PosDiffPDCO{e1,e2}	produces positions at which e1 and e2 differ
#
#   PositionsPDCO{e, i}	generates the positions at which i occurs in e.
#  
#   RandomPDCO{e1,e2, ...}
#			produces results of e1, e2, ... at random
#
#   ReducePDCO{op, x, e}
#			"reduces" the sequence e by starting with the value x
#			and repetitively applying op to the current
#		  	value and values from e.
#
#   RemoveDuplPDCO{e}	removes duplicate adjacent values.
#  
#   RepaltPDCO{e}	models |e
#
#   RepeatPDCO{e1, e2}	repeats the sequence for e1 e2 times
#
#   ReplPDCO{e1,e2}	replicates each value in e1 by the corresponding
#   		   	integer value in e2.
#  
#   ResumePDCO{e1,e2,e3}
#			models every e1 \ e2 do e3
#
#   ReversePDCO{e, i}  	produces the results of e in reverse order. If i
#			is given, e is truncated to i values.
#  
#   RotatePDCO(e, i)	rotates the sequence for e left by i; negative
#   		   	i rotates to the right
#
#   SelfreplPDCO{e1,i}	produces e1 * i copies of e1
#
#   SeqlistPDCO{e1, i}	produce list with first i values of e1; i
#			defaults to all values
#  
#   SimpleAER{e1,e2, ...}
#			simple evaluation with only success or
#			failure
#
#   SkipPDCO{e1,e2}	generate e1 skipping each e2 terms
#
#   SmodPDCO{e1,e2}	reduce terms in e1 (shaft) modulus e2
#
#   SpanPDCO{e,m}	fill in between consecutive (integer) values in
#			e % m; m default 100
#
#   SumlimitPDCO{e, i, j}
#			produces values of e until their sum exceeds
#			i.  Values less than j are discarded.
#
#   TrinopPDCO{op,e2,e2,e3}
#			produces the result of applying op to e1, e2, and e3
#
#   UndulantPDCO{e}	produces the undulant for e.
#
#   UniquePDCO{e}	produces the unique results of e in the order
#			they first appear
#
#   UnopPDCO{e1,e2}	produces the result of applying e1 to e2
#
#   UpperTrimPDCO{e}	upper trim
#
#   ValrptPDCO{e1,e2}	synonym for ReplPDCO
#
#   WobblePDCO{e}	produces e(1), e(2), e(1), e(2), e(3), e(2), ...
#  
#   Comments:
#
#   Because of the handling of the scope of local identifiers in
#   co-expressions, expressions in programmer-defined control
#   operations cannot communicate through local identifiers.  Some
#   constructions, such as break and return, cannot be used in argu-
#   ments to programmer-defined control operations.
#  
############################################################################
#
#  Requires:  co-expressions
#
############################################################################
#
#  Links:  lists, periodic, rational
#
############################################################################

link lists
link periodic
link rational

procedure AddTabbyPDCO(L)	#: PDCO to add tabby to treadling
   local i

   i := @L[2] | 4		# number of regular treadles

   suspend InterPDCO([L[1], create |((i + 1) | (i + 2))])

end

procedure AllparAER(L)		#: PDAE for parallel evaluation with repeats
   local i, L1, done

   L1 := list(*L)

   done := list(*L,1)

   every i := 1 to *L do L1[i] := @L[i] | fail

   repeat {
      suspend L1[1] ! L1[2:0]
      every i := 1 to *L do
         if done[i] = 1 then ((L1[i] := @L[i]) | (done[i] := 0))
      if not(!done = 1) then fail
      }

end

procedure AltPDCO(L)		#: PDCO to model alternation

   suspend |@L[1]
   suspend |@L[2]

end

procedure BinopPDCO(L)		#: PDCO to apply binary operation to sequences
   local op, x, y

   repeat {
      op := @L[1]
      op := proc(op, 2) | fail
      (x := @L[2] & y := @L[3]) | fail
      suspend op(x, y)
      }

end

procedure CFapproxPDCO(L)	#: PDCO for continued-fraction approximations
  local prev_n, prev_m, n, m, t

  prev_n := [1]
  prev_m := [0, 1]

  put(prev_n, (@L[1]).denom) | fail

  while t := @L[1] do {
     n := t.denom * get(prev_n) + t.numer * prev_n[1]
     m := t.denom * get(prev_m) + t.numer * prev_m[1]
     suspend rational(n, m, 1)
     put(prev_n, n)
     put(prev_m, m)
     if t.denom ~= 0 then {		# renormalize
        every !prev_n /:= t.denom
        every !prev_m /:= t.denom
        }
     }

end

procedure ComparePDCO(L)	#: PDCO to compare sequences
   local x1, x2

   while x1 := @L[1] do
      (x1 === @L[2]) | fail
   if @L[2] then fail else return

end

procedure ComplintPDCO(L)	#: PDCO to generate integers not in sequence
   local i, j			# EXPECTS MONOTONE NON-DECREASING SEQUENCE

   j := 0

   while i := @L[1] do {
      i := integer(i) | stop("*** invalid value in sequence to Compl{}")
      suspend j to i - 1
      j := i + 1
      }

   suspend seq(j)

end

procedure CondPDCO(L)		#: PDCO for generalized Lisp conditional
   local i, x

   every i := 1 to *L do
      if x := @L[i] then {
         suspend x
         suspend |@L[i]
         fail
         }

end

procedure CumsumPDCO(L)		#: PDCO to produce cumulative sum
   local i

   i := 0

   while i +:= @L[1] do
      suspend i

end

procedure CycleparAER(L)	#: PDAE for parallel evaluation with cycling
   local i, L1, done

   L1 := list(*L)

   done := list(*L,1)

   every i := 1 to *L do L1[i] := @L[i] | fail

   repeat {
      suspend L1[1]!L1[2:0]
      every i := 1 to *L do {
         if not(L1[i] := @L[i]) then {
            done[i] := 0
            if !done = 1 then {
               L[i] := ^L[i]
               L1[i] := @L[i] | fail
               }
            else fail
            }
         }
      }
end

procedure DecimatePDCO(L)	#: PDCO to decimate sequence
   local i, j, count

   count := 0

   while j := @L[2] do {
      while i := @L[1] | fail do {
         count +:= 1
         if count = j then break next
         else suspend i
         }
      }

end

procedure DecimationPDCO(L)	#: PDCO to create decimation sequence
   local i, count

   count := 0

   while i := @L[1] do {
      count +:= 1
      if i % 2 = 1 then suspend count
      }

end
procedure DecollatePDCO(L)	#: PDCO to decollate sequence
   local i, j, x

   i := @L[2] | 1

   i %:= 2
 
   j := 0

   while x := @L[1] do {
      j +:= 1
      if j % 2 = i then suspend x
      }

end

procedure DeltaPDCO(L)		#: PDCO to generate difference sequence 
   local i, j

   i := @L[1] | fail

   while j := @L[1] do {
      suspend j - i
      i := j
      }

end

procedure ElevatePDCO(L)	#: PDCO to elevate sequence
   local n, m, shafts, i, j, k

   m := @L[2] | fail
   n := @L[3] | fail

   shafts := list(m)

   every !shafts := []

   every i := 1 to m do
      every put(shafts[i], i to n by m)

   while j := @L[1] do {
      i := j % m + 1
      k := get(shafts[i])
      suspend k
      put(shafts[i], k)
      }

end

procedure EveryPDCO(L)		#: PDCO to model iteration

   while @L[1] do @^L[2]

end

procedure ExtendSeqPDCO(L)	#: PDCO to extend sequence
   local count

   count := integer(@L[2]) | fail
   if count < 1 then fail

   repeat {
      suspend |@L[1] do {
         count -:= 1
         if count = 0 then fail
         }
      if *L[1] == 0 then fail
      L[1] := ^L[1]
      }

end

procedure ExtractAER(L)		#: PDAE to extract values
   local i, j, n, L1

   L1 := list(*L/2)

   repeat {
      i := 1
      while i < *L do {
         n := @L[i] | fail
         every 1 to n do
            L1[(i + 1)/2] := @L[i + 1] | fail
         L[i + 1] := ^L[i + 1]
         i +:= 2
         }
      suspend L1[1] ! L1[2:0]
      } 

end

procedure FifoAER(L)		#: PDAE for reversal of lifo evaluation
   local i, L1, j

   L1 := list(*L)

   j := *L

   repeat {
      repeat {
         if L1[j] := @L[j]
         then {
            j -:= 1
            (L[j] := ^L[j]) | break
            }
         else if (j +:= 1) > *L then fail
         }
      suspend L1[1] ! L1[2:0]
      j := 1
      }

end

procedure FriendlyPDCO(L)	# PDCO for friendly sequences
   local mod, state, value

   mod := @L[1] | fail
   state := @L[2]
   if /state then state := ?mod

   repeat {
      suspend state
      value := @L[3] | fail
      if value % 2 = 0 then state +:= 1
      else state -:= 1
      state := residue(state, mod, 1)
      }

end

procedure GaltPDCO(L)		#: PDCO to concatenate sequences
   local C

   every C := !L do
      suspend |@C

end

procedure GconjPDCO(L)		#: PDCO for generalized conjunction

   suspend Gconj_(L,1)

end

procedure Gconj_(L,i,v)

   local e
   if e := L[i] then {
      suspend v:= |@e & Gconj_(L,i + 1,v)
      L[i] := ^e
      }
   else suspend v

end

procedure HistoPDCO(L)		#: histogram
   local limit, results, seq

   limit := @L[2] | 100

   seq := []

   while put(seq, @L[1])

   results := list(max ! seq, 0)

   every results[!seq] +:= 1

   suspend !results

end


procedure IncreasingPDCO(L)	#: PDCO to filter out non-increasing values
   local last, current

   last := @L[1] | fail

   suspend last

   while current := @L[1] do {
      if current <= last then next
      else {
         suspend current
         last := current
         }
      }

end

procedure IndexPDCO(L)		#: PDCO to select terms by position
   local i, j, x

   j := @L[2] | fail

   every i := seq() do {	# position
      x := @L[1] | fail
      if j = i then {
         suspend x
         repeat {
            j := @L[2] | fail
            if j > i then break
            }
         }
      }

end

procedure InterPDCO(L)		#: PDCO to interleave sequences

   suspend |@!L

end

procedure LcondPDCO(L)		#: PDCO for Lisp conditional
   local i

   every i := 1 to *L by 2 do
      if @L[i] then {
         suspend |@L[i + 1]
         fail
         }

end

procedure LengthPDCO(L)		#: PDCO to produce length of sequence
   local i

   i := 0

   while @L[1] do i +:= 1

   return i

end

procedure LifoAER(L)		#: PDAE for standard lifo evaluation
   local i, L1, j

   L1 := list(*L)

   j := 1

   repeat {
      repeat
         if L1[j] := @L[j]
         then {
            j +:= 1
            (L[j] := ^L[j]) | break
            }
         else if (j -:=  1) = 0
            then fail
      suspend L1[1] ! L1[2:0]
      j := *L
      }

end

procedure LimitPDCO(L)		#: PDCO to model limitation
   local i, x

   while i := @L[2] do {
      every 1 to i do
         if x := @L[1] then suspend x
         else break
      L[1] := ^L[1]
      }

end

procedure ListPDCO(L)		#: list from sequence
   local limit, result

   limit := @L[2] | 100

   result := []

   every put(result, |@L[1]) \ limit

   return result

end

procedure LowerTrimPDCO(L)	#: lower trimming
   local i

   while i := @L[1] do {
      i -:= 1
      if i ~= 0 then suspend i
      }

end

procedure MapPDCO(L)		#: PDCO to map values
   local maptbl, x

   maptbl := table()

   while x := @L[1] do {
      /maptbl[x] := (@L[2] | fail)
      suspend maptbl[x]
      }

end

procedure OddEvenPDCO(L)	#: PDCO to force odd/even sequence
   local val, val_old

   while val := @L[1] do {
      if val % 2 = \val_old % 2 then
         suspend val_old + 1
      suspend val
      val_old := val
      }

end

procedure PalinPDCO(L)		#: PDCO to produce palindromic sequence
   local tail, x

   tail := []

   while x := @L[1] do {
      suspend x
      push(tail, x)
      } 

   every suspend !tail

end

procedure ParallelPDCO(L)	#: synonym for Inter

   ParallelPDCO := InterPDCO		# redefine for next use

   suspend InterPDCO(L)

end

procedure ParallelAER(L)	#: PDAE for parallel evaluation
   local i, L1

   L1 := list(*L)

   repeat {
      every i := 1 to *L do
         L1[i] := @L[i] | fail
      suspend L1[1] ! L1[2:0]
      }

end

procedure PatternPalinPDCO(L)	#: PDCO to produce pattern palindrome
   local tail, x, limit

   tail := []

   limit := @L[2] | (2 ^ 15)	# good enough

   every 1 to limit do {
      x := @L[1] | break
      suspend x
      push(tail, x)
      } 

   get(tail)

   pull(tail)

   every suspend !tail

end

procedure PeriodPDCO(L)		#: PDCO for periodic part of sequence
   local limit, result

   limit := @L[2] | 300

   result := []

   every put(result, |@L[1]) \ limit

   result := repeater(result)

   suspend !result[2]

end

procedure PermutePDCO(L)	#: PDCO for permutations
   local temp1, temp2, chunk, i, x

   repeat {
      temp1 := @L[2] | fail
      temp2 := []
      every put(temp2, i := 1 to *temp1)
      chunk := []
      every 1 to i do
         put(chunk, @L[1]) | fail
      suspend !lmap(temp1, temp2, chunk)
      }

end

procedure PivotPDCO(L)		#: PDCO to generate pivot points
   local current, direction, m, new

   m := @L[2]
   /m := 100
   direction := "+"

   current := @L[1] % m | fail

   suspend current

   repeat {
      new := @L[1] % m | break
      if new = current then next
      case direction of {
         "+":  {
            if new > current then {
               current := new
               next
               }
            else {
               suspend current
               current := new
               direction := "-"
               }
            }
         "-":  {
            if new < current then {
               current := new
               next
               }
            else {
               suspend current
               current := new
               direction := "+"
               }
            }
         }

      }

   return current

end

procedure PositionsPDCO(L)	# positions in e of i
   local i, count, j

   i := integer(@L[2]) | fail

   count := 0

   while j := @L[1] do {
      count +:= 1
      if j = i then suspend count
      }

end

procedure PosDiffPDCO(L)	# PDCO to generate positions of difference
   local i, x, y

   i := 0

   while x := @L[1] & y := @L[2] do {
      i +:= 1
      if x ~=== y then suspend i
      }

end

procedure RandomPDCO(L)		#: PDCO to generate from sequences at random
   local x

   while x := @?L do suspend x

end

procedure RepaltPDCO(L)		#: PDCO to model repeated alternation
   local x

   repeat {
      suspend |@L[1]
      if *L[1] == 0 then fail
      L[1] := ^L[1]
      }

end

procedure ReducePDCO(L)		#: PDCO to reduce sequence using binary operation
   local op, x

   op := proc(@L[1], 2) | stop("*** invalid operation for Reduce{}")
   x := @L[2] | fail

   while x := op(x, @L[3])

   return x

end

procedure RepeatPDCO(L)		#: PDCO to repeat sequence
   local i, x

   while i := @L[2] do {
      if not(i := integer(i)) then stop("*** invalid repetition in Repeat{}")
      every 1 to i do {
         suspend |@L[1]
         L[1] := ^L[1]
         }
      }

end

procedure RemoveDuplPDCO(L)	#: PDCO for remove duplicate values in a sequence
   local old, new

   old := @L[1] | fail
   suspend old

   repeat {
      new := @L[1] | fail
      if new === old then next
      else {
         suspend new
         old := new
         }
      }

end

procedure ReplPDCO(L)		#: PDCO to replicate values in a sequence
   local x, i

   i := 1		# default

   while x := @L[1] do {
      i := @L[2]
      suspend (1 to i) & x
      }

end

procedure ResumePDCO(L)		#: PDCO to model limited iteration
   local i

   while i := @L[2] do {
      L[1] := ^L[1]
      every 1 to i do if @L[1] then @^L[3] else break
      }

end

procedure ReversePDCO(L)	#: PDCO to reverse sequence
   local result, limit

   result := []

   limit := @L[2]

   /limit := 2 ^ 15		# enough

   every 1 to limit do
       push(result, @L[1]) | break

   suspend !result

end

procedure RotatePDCO(L)		#: PDCO to rotate sequence
   local result, i, x

   i := integer(@L[2]) | stop("*** invalid specification in Rotate{}")

   result := []

   if i <= 0 then {		# if not to right, works for infinite sequence
      every 1 to -i do
         put(result, @L[1]) | break
      while x := @L[1] do
         suspend x
      suspend !result
      }

   else {
      while put(result, @L[1])
      suspend !lrotate(result, i)
      }

end

procedure SelfreplPDCO(L)	#: PDCO to produce multiple of values in sequence
   local i, j

   j := @L[2] | 1
   j := integer(j) | stop("*** invalid second argument to Selfrepl{}")

   while i := @L[1] do {
      i := integer(i) | stop("*** invalid value in Selfrepl{}")
      suspend (1 to i * j) & i
      }

end

procedure SeqlistPDCO(L)	#: PDCO to return list of values
   local result, limit

   result := []

   limit := @L[2] | 2 ^ 15	# crude ...

   every 1 to limit do
      put(result, @L[1]) | break

   return result

end

procedure SimpleAER(L)		#: PDAE for simple evaluation
   local i, L1

   L1 := list(*L)

   every i := 1 to *L do
      L1[i] := @L[i] | fail

   return L1[1] ! L1[2:0]

end

procedure SkipPDCO(L)		#: PDCO to skip terms
   local gap

   suspend @L[1]

   repeat {
      gap := @L[2] | fail
      every 1 to gap do
         @L[1] | fail
      suspend @L[1]
      }

end

procedure SmodPDCO(L)		#: generalized modular reduction
   local i, m

   while i := @L[1] do {
      m := @L[2] | fail
      suspend residue(i, m, 1)
      }

end

procedure SpanPDCO(L)		#: fill in gaps in integer sequences
   local i, j, m

   j := @L[1] | fail

   m := @L[2]
   /m := 100

   while i := residue(@L[1], m, 1) do {
      if i > j then suspend j to i - 1
      else if i < j then suspend j to i + 1 by -1
      j := i
      } 

   suspend j

end

procedure SumlimitPDCO(L) 	#: PDCO to sum sequence to a limit
   local sum, min, limit, i

   limit := integer(@L[2]) | 2 ^ 15
   min := integer(@L[3]) | 0
   sum := 0

   while i := @L[1] do {
      if i < min then next
      if (sum + i) > limit then fail
      sum +:= i
      suspend i
      }

end

procedure TrinopPDCO(L)		#: PDCO to apply trinary operator to sequences
   local op, x, y, z

   repeat {
      op := proc(@L[1], 3) | fail
       x := @L[2] & y := @L[3] & z := @L[4] | fail
      suspend op(x, y, z)
      }

end

procedure UndulantPDCO(L)	#: PDCO to produce undulant
   local i, j, dir

   i := @L[1] | fail

   suspend i			# first value always is in undulant

   j := i			# last term in undulant

   while i := @L[1] do {	# get initial direction
      if i > j then {
         dir := -1
         break
         }
      else if i < j then {
         dir := 1
         break
         }
     } 

   j := i

   while i := @L[1] do {
      if i < j then  {
         if dir = -1 then {
            suspend j
            j := i
            dir := 1
            }
         else j := i
         }
      if i > j then {
         if dir = 1 then {
            suspend j
            j := i
            dir := -1
            }
         else j := i
         }
      }

   fail

end

procedure UniquePDCO(L)		#: PDCO to filter out duplication values
   local done, x

   done := set()

   while x := @L[1] do
      if member(done, x) then next
      else {
         insert(done, x)
         suspend x
         }

end

procedure UnopPDCO(L)		#: PDCO to apply unary operation to sequence
   local op, x

   repeat {
      op := @L[1]
      op := proc(op, 1) | fail
      x := @L[2]  | fail
      suspend op(x)
      }

end

procedure UpperTrimPDCO(L)	#: upper sequence trimming
   local done, i

   done := set()

   while i := @L[1] do {
      if not member(done, i) then
         insert(done, i)
      else suspend i
      }

end

procedure ValrptPDCO(L)		#: synonym for Repl

   ValrptPDCO := ReplPDCO

   suspend ReplPDCO(L)

end

procedure WobblePDCO(L)		#: PDCO to produce sequence values alternately
   local x, y

   x := @L[1] | fail
   suspend x

   while y := @L[1] do {
      suspend y | x | y
      x := y
      }

end