如何使用show在没有警告的情况下打印haskell中的Integeral值?

时间:2015-01-26 07:08:19

标签: haskell warnings show ghci integral

当我运行以下启用了警告的haskell代码时

module Main where
main :: IO()
main = interact (unlines.strout.calc.extinps.words)

--calculates factorial
factorial :: Integral a=> a->a
factorial n = product [1..n]

--Extracts numbers from the input
extinps ::(Read b)=>[String]->[b]
extinps x=map read x

--Calculates the factorial
calc :: (Integral b) => [b] -> [b]
calc x= map factorial x

--Converts the result to a string
strout::(Show b)=>[b]->[[Char]]
strout x=[show a|a<-x]

警告后我收到警告:

factout.hs:3:26: Warning:
Defaulting the following constraint(s) to type `Integer'
   (Show b0) arising from a use of `strout' at factout.hs:3:26-31
   (Read b0) arising from a use of `extinps' at factout.hs:3:38-44
   (Integral b0) arising from a use of `calc' at factout.hs:3:33-36    
In the first argument of `(.)', namely `strout'                       
In the second argument of `(.)', namely                                 
   `strout . calc . extinps . words'                                   
In the first argument of `interact', namely                               
   `( unlines . strout . calc . extinps . words)'
Ok, modules loaded: Main.                                            

我如何摆脱它?

1 个答案:

答案 0 :(得分:7)

编译器不知道应该使用哪个Integral的具体实现。 猜测 Integer就可以了。

如果您注释例如calc,警告将消失:

main :: IO ()
main = interact (unlines . strout . (calc :: [Integer] -> [Integer]) . extinps . words)