home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e031 / 3.ddi / MATHZIP2 / STARTUP / SEQUENCE.M < prev    next >
Encoding:
Text File  |  1991-09-23  |  2.2 KB  |  78 lines

  1.  
  2. (* SequencePosition.m    Bruce Smith    6 Dec 89, 29 Apr 90, 25 Jun 90 *)
  3.  
  4. Begin["System`"]
  5.  
  6. SequencePosition::usage = "SequencePosition[sequence, pattern] gives a list
  7. of the positions of the subsequences of sequence which match pattern.
  8. The positions are in the form
  9. {index of first element, index of last element},
  10. which is a standard sequence specification acceptable to Take or Drop."
  11.  
  12. FirstSequencePosition::usage = "FirstSequencePosition[sequence, pattern]
  13. gives the position of the first subsequence of sequence which matches
  14. pattern, or Null if there is no such subsequence.
  15. The position is in the form
  16. {index of first element, index of last element},
  17. which is a standard sequence specification acceptable to Take or Drop."
  18.  
  19. SequencePosition::pattern = FirstSequencePosition::pattern =
  20. "Pattern `1` in `2` must be a list (for example, {_Integer, __Real})."
  21.  
  22. Begin["`Private`"]
  23.  
  24. (*
  25. Unprotect[SequencePosition, FirstSequencePosition]
  26. Clear[SequencePosition, FirstSequencePosition]
  27. *)
  28.  
  29. SequencePosition[seq_,{pat___}] :=
  30.     Module[{flattenme}, Module[{res = flattenme[], seqlen = Length[seq]},
  31.     Replace[seq, _[a___,pat,b___] -> Null /;
  32.         (res = flattenme[res, {1+Length[Hold[a]],
  33.                 seqlen - Length[Hold[b]] }];
  34.          False) ];
  35.     Apply[List, Flatten[res, Infinity, flattenme]]
  36. ]]
  37.  
  38. e:SequencePosition[seq_, otherpat_] := e /; (
  39.     With[{h = Head[Unevaluated[e]]},
  40.         Message[h::pattern, otherpat, Unevaluated[e]]];
  41.     True
  42.     )
  43.  
  44. e:SequencePosition[a___ /; Length[Hold[a]] != 2] := e /; (
  45.     With[{h = Head[Unevaluated[e]]},
  46.         Message[h::argct, h, Unevaluated[e],
  47.             Length[Unevaluated[e]] ] ];
  48.     True
  49.     )
  50.  
  51. FirstSequencePosition[seq_,{pat___}] :=
  52.     Module[{res = Null, seqlen = Length[seq]},
  53.     Replace[seq, _[a___,pat,b___] -> Null /;
  54.         (Set[res, {1+Length[Hold[a]],
  55.                 seqlen - Length[Hold[b]] }];
  56.          True) ];
  57.     res
  58. ]
  59.  
  60. e:FirstSequencePosition[seq_, otherpat_] := e /; (
  61.     With[{h = Head[Unevaluated[e]]},
  62.         Message[h::pattern, otherpat, Unevaluated[e]]];
  63.     True
  64.     )
  65.  
  66. e:FirstSequencePosition[a___ /; Length[Hold[a]] != 2] := e /; (
  67.     With[{h = Head[Unevaluated[e]]},
  68.         Message[h::argct, h, Unevaluated[e],
  69.             Length[Unevaluated[e]] ] ];
  70.     True
  71.     )
  72.  
  73. Protect[SequencePosition, FirstSequencePosition]
  74. End[]
  75. End[]
  76.  
  77. Null
  78.