Code Snippets APL

Short texts APL 🧑‍💻 Code snippets APL ✍️ Reading lists APL 👀

Knuth-Morris-Pratt prefix (⎕IO←0)

∇ P←prefix s;m;i;j m←≢s P←0⍴⍨m j←0 :for i :in 1+⍳¯1+m :while j≥0 :if s[j]=s[i] :leave :endif :if (j-1)≥0 j←P[j-1] :else j←¯1 :endif :endwhile j+←1 P[i]←j :endfor ∇