Type error in function binding

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Type error in function binding

bahadýr altan
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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120402/dfb3fd9e/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

Type error in function binding

Mats Rauhala
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120402/71ffd3b2/attachment.pgp>

Reply | Threaded
Open this post in threaded view
|

Type error in function binding

Markus Läll-2
In reply to this post by bahadýr altan
You're missing some parenthesis. Instead of "mycounter tail st" write
"mycounter (tail st)".

On Mon, Apr 2, 2012 at 5:50 PM, bahad?r altan <doaltan at yahoo.co.uk> 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?
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



--
Markus L?ll


Reply | Threaded
Open this post in threaded view
|

Type error in function binding

Nathan Hüsken
In reply to this post by bahadýr altan
I think you are  just missing parentheses:
mycounter st =
if st == [] then 0
else if head st == 'a' then 1 + mycounter (tail st) else mycounter (tail st)

On 04/02/2012 04:50 PM, 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?
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners