
On 15:50 Mon 02 Apr , bahadýr altan wrote:
Hi. I'm trying to write a code which counts the number of 'a's in a string. it looks like this :
mycounter st = if st == [] then 0 else if head st == 'a' then 1 + mycounter tail st else mycounter tail st
but it gives this error : ERROR "deneme.hs":19 - Type error in function binding *** Term :mycounter *** Type : [Char] -> b *** Does not match : ([a] -> [a]) -> [Char] -> b *** Because : unification would give infinite type
I have no idea about what I'm doing wrong. Can you help me with this please?
I didn't verify, but it could be that you need parenthesize the `counter tail st` part. A different kind of solution could be: counter [] = 0 counter ('a':xs) = 1 + counter xs counter (_:xs) = counter xs Or maybe even: `length $ filter (== 'a') xs` -- Mats Rauhala MasseR