I have a code which works fine:
let rec calculate s l acc =
if length s = 0 then
acc
else
if first s = l then
calculate (rest s) l (acc+1)
else
calculate (rest s) (first s) acc
I want to rewrite it using pattern matching:
let rec calculate s l acc =
function
| _, _, _ when length s = 0 -> acc
| _, _, _ when first s = l -> calculate (rest s) l (acc+1)
| _, _, _ -> calculate (rest s) (first s) acc
But last function returns the error message:
| _, _, _ when first s = l -> calculate (rest s) l (acc+1) -----------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^/Users/demas/temporary/stdin(512,36): error FS0001: Type mismatch. Expecting a 'a but given a 'b * 'c * 'd -> 'a The resulting type would be infinite when unifying ''a' and ''b * 'c * 'd -> 'a'
Why ?
elifinstead ofelse ifto avoid too deep indentation, but that's it. - Tarmil