Вот решение, используя рекурсивные функции:
/// Wraps the recursive findMatches function defined inside, so that you don't have to seed it with the "internal" paramters
let findMatches chars str =
/// Returns whether or not the string matches the beginning of the character array
let rec isStartMatch chars (str: string) =
match chars with
| char :: rest when str.Length > 0 ->
char = str.[0] && (isStartMatch rest str.[1..(str.Length - 1)])
| _ -> str.Length = 0
/// The actual function here
let rec findMatches matchedIndices i chars str =
match chars with
| _ :: rest ->
if isStartMatch chars str
then findMatches (i :: matchedIndices) (i + 1) rest str
else findMatches matchedIndices (i + 1) rest str
| [] -> matchedIndices
findMatches [] 0 chars str
не самым эффективным, как она перебирает символы дважды, если они являются частью матча, но это не очень большое беспокойство.
Можете ли вы показать, где вы застряли? –
Я попытался использовать List.FindIndex, но это возвращает только первый индекс – TJF
, если он вернет вас 9? – Rodion