1

I have a function which has this type by default :

func :: Integer -> (Integer,Integer) -> [[String]] -> ([Char],[Char],[Char],[Char]) -> (Integer,Integer)

But I want it to return (Int,Int) When I wrote this :

func:: Integer -> (Integer,Integer) -> [[String]] -> ([Char],[Char],[Char],[Char]) -> (Int,Int) 

I get this error : Main> :l play

ERROR "play.hs":64 - Type error in explicitly typed binding
*** Term           : func
*** Type           : Integer -> (Integer,Integer) -> [[String]] -> ([Char],[Char],[Char],[Char]) -> (Integer,Integer)
*** Does not match : Integer -> (Integer,Integer) -> [[String]] -> ([Char],[Char],[Char],[Char]) -> (Int,Int)

How can I fix this? Thanks.

4

1 回答 1

3

Write a new wrapper function to wrap func, then use the wrapper function instead.

func' :: Integer ->
         (Integer,Integer) ->
         [[String]] ->
         ([Char],[Char],[Char],[Char]) ->
         (Int,Int)
func' a b c d = (fromInteger x, fromInteger y) where
    (x, y) = func a b c d

Alternatively, you could insert the calls to fromInteger directly into func.

The issue here is that Int and Integer are different types, and the compiler does not convert between them implicitly --- you have to do so explicitly, hence the calls to fromInteger. fromInteger converts from Integer to any numeric type.

于 2013-03-30T14:38:27.357 回答