// XY pattern matching and substitution ; pattern { [s t c] s t from c into } ; ; Pattern { [c] "abcdefghijklmnopqrstuvxyz" "\$" [,] each\ 0s \$. c pattern } ; ; from { [s t] [[] s] [t] [from.] over first reverse flip } ; ; from. { [[r s] t] t atom? [r s t from-atom] [r s t from-list] if } ; ; from-list { [r s t] r s first pair t [from.] over first 1 s _ pair } ; ; from-atom { [r s t] t from-upper? [r s t from-A] [r s t from-a] if } ; ; from-a { [r s t] s uncons [t swap pair r cons] dip pair } ; ; from-A { [r s t] [t s] r cons [] pair } ; ; from-upper? string first _ic dup 64 > swap 91 < & ; ; into { [[s v] c] S/ [s v] c into. S\ first } ; ; into. { [[s v] c] \c atom? [v \c , s \c ? @] [[s v] \c [_z] each/] if } ; ; << { [] c\ _x _y >> take-drop <<. -> } ; ; <<. { [x [t C] y] x t <<.. t C pattern 1 y _ , } ; ; <<.. { [x t] 0 x count t count - pair x _ i [i] dip } ; ; >> ; \\ [10 20 30] [a\$ b\$ c\$] from [10 [20 30] [40 50 60 70] 80] [a\$ b\$ [c\$ D\$] e\$] from [10 20 30] [a\$ b\$ c\$] [a\$ b\$ + c\$ *] pattern [10 20 30] [a\$ b\$ + c\$ *] Pattern 10 20 30 40 50 << [a\$ b\$ c\$] a\$ b\$ + c\$ * >> 60 70 80