Reputation: 1
I'm coding a helper function for encodeHelper but cannot seem to get the pattern matching to work in the recursive call. Does anyone know how to fix this error?
Please look at the code and error message in UTOP
let rec encodeHelper (l : 'a list) (curr : ('a * int) list) (currElement : ('a * int) option) =
match l, currElement with
| [], None -> curr
| [], Some(k) as currElement -> curr @[(fst k, snd k)]
| hd1::tl, None -> encodeHelper tl curr (k, 1)
| hd::tl, Some(k) ->
let element_ = fst k and
amount = snd k in
if element_ = hd
then encodeHelper tl curr (element_, amount+1)
else
encodeHelper tl (curr @ [(element_, amount)]) (hd, 1)
The error message is that (k,1)
in the recursive call on the 5th line is This expression has type 'b * 'c but an expression was expected of type ('a * int) option
Let me know if you have tips / ways to fix this!
Upvotes: 0
Views: 66
Reputation: 66818
The compiler is telling you that this expression is wrong:
encodeHelper tl curr (k, 1)
Indeed, the third argument of encodeHelper
is specified to be of type ('a * int) option
and the expression (k, 1)
can't possiblly be of this type. An option type is None
or Some x
.
Possibly it will work better if you replace (k, 1)
with (Some (k, 1))
. However I don't see a definition of k
in this branch of the match.
Upvotes: 2