简体   繁体   English

在Haskell中更改函数类型

[英]Changing function type in Haskell

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 : 但我希望它返回(Int,Int)当我写这个:

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

I get this error : Main> :l play 我收到这个错误: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. 谢谢。

Write a new wrapper function to wrap func , then use the wrapper function instead. 编写一个新的包装函数来包装func ,然后使用包装函数代替。

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 . 或者,您可以将对fromInteger的调用直接插入到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 . 这里的问题是IntInteger是不同的类型,并且编译器不会隐式地在它们之间进行转换---你必须明确地这样做,因此调用fromInteger fromInteger converts from Integer to any numeric type. fromIntegerInteger转换为任何数字类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM