Created
April 5, 2025 07:35
-
-
Save altbodhi/25bcff2d059113de18e0a123501682b1 to your computer and use it in GitHub Desktop.
Wirt Niklaus Example search in string via F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Text = | |
/// поиск индекс первого вхождения образца в строке | |
let findFirst (s: string) (p: string) = | |
// предикат проверяющий совпадение части строки с образцом | |
let eqSample i (s: string) (p: string) = | |
let rec loop j = | |
if j < p.Length && p.[j] = s.[i + j] then | |
loop (j + 1) | |
else | |
j | |
loop 0 | |
let rec loop i = | |
if i <= s.Length - p.Length then | |
let dx = eqSample i s p | |
if dx = p.Length then i // нашли подстроку | |
else loop (i + dx + 1) | |
else -1 // Возвращаем -1, если не найдено | |
if p.Length > s.Length || p.Length = 0 | |
then -1 | |
else loop 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment