Just getting into functional programming and F# with the most appropriately titled Functional Programming Using F#. I wrote the following function definition for problem 2.4 but I'm thinking there's most likely a more elegant and/or idiomatic solution:
let rec occurrencesFromIth =
let bool2int x = if x then 1 else 0
function
| str:string, i, _ when i >= str.Length -> 0
| str, i, ch ->
isIthChar (str,i,ch ) |> bool2int |> (+) (occurrencesFromIth (str,i+1,ch ))
isIthChar
is defined as:
let isIthChar ( str:string, i, ch ) = str.[i] = ch
The actual problem description is:
Declare the F# function
occFromIth
:
string * int * char -> int
whereoccFromIth(str,i,ch)
= the number of occurrences of characterch
in positionsj
in the stringstr
withj >= i
.Hint: the value should be 0 for
i >= size str
.