无注释的Haskell读取函数

无注释的Haskell读取函数,haskell,Haskell,haskell read函数是否隐式地将一种数据类型转换为另一种数据类型 import IO main = do putStrLn "Enter an interger: " num <- getLine putStr "Value + 3 is " putStrLn (show((read num) + 3)) 导入IO main=do putStrLn“输入一个整数:” numread具有typereada=>

haskell read函数是否隐式地将一种数据类型转换为另一种数据类型

import IO

main = do
    putStrLn "Enter an interger: "
    num <- getLine
    putStr "Value + 3 is "
    putStrLn (show((read num) + 3))
导入IO
main=do
putStrLn“输入一个整数:”

num
read
具有type
reada=>String->a
,这意味着它可以将
String
转换为属于
read
type类实例的任何类型。它将使用哪种类型取决于周围的代码。在本例中,您将写入
读取num+3
+
运算符也被重载,以用于作为
Num
实例的任何类型,因此
read Num+3
的类型是
(read a,Num a)=>a
。Haskell有一种机制,每当
Num
type类保留在表达式的最终类型中时,它都会选择默认类型。在这种情况下,它默认为
Integer
,因此最后的类型是
Integer

通过启用
-Wtype defaults
警告,您可以看到这一点,该警告也包含在
-Wall
中:

Test.hs:5:15: warning: [-Wtype-defaults]
    • Defaulting the following constraints to type ‘Integer’
        (Show a0) arising from a use of ‘show’ at Test.hs:5:15-34
        (Num a0) arising from a use of ‘+’ at Test.hs:5:20-33
        (Read a0) arising from a use of ‘read’ at Test.hs:5:21-28
    • In the first argument of ‘putStrLn’, namely
        ‘(show ((read num) + 3))’
      In a stmt of a 'do' block: putStrLn (show ((read num) + 3))
      In the expression:
        do putStrLn "Enter an interger: "
           num <- getLine
           putStr "Value + 3 is "
           putStrLn (show ((read num) + 3))
  |
5 |     putStrLn (show((read num) + 3))
  |      
Test.hs:5:15:警告:[-Wtype默认值]
•将以下约束默认为“整型”
(显示a0)因在试验中使用“显示”而产生。hs:5:15-34
(数值a0)因在试验中使用“+”而产生。hs:5:20-33
(读取a0)因在试验中使用“读取”而产生。hs:5:21-28
•在“putStrLn”的第一个参数中,即
“(显示((读取数量)+3))”
在“do”块的stmt中:putStrLn(show((read num)+3))
在表达式中:
输入一个整数:

num
read
具有type
reada=>String->a
,这意味着它可以将
String
转换为属于
read
type类实例的任何类型。它将使用哪种类型取决于周围的代码。在本例中,您将写入
读取num+3
+
运算符也被重载,以用于作为
Num
实例的任何类型,因此
read Num+3
的类型是
(read a,Num a)=>a
。Haskell有一种机制,每当
Num
type类保留在表达式的最终类型中时,它都会选择默认类型。在这种情况下,它默认为
Integer
,因此最后的类型是
Integer

通过启用
-Wtype defaults
警告,您可以看到这一点,该警告也包含在
-Wall
中:

Test.hs:5:15: warning: [-Wtype-defaults]
    • Defaulting the following constraints to type ‘Integer’
        (Show a0) arising from a use of ‘show’ at Test.hs:5:15-34
        (Num a0) arising from a use of ‘+’ at Test.hs:5:20-33
        (Read a0) arising from a use of ‘read’ at Test.hs:5:21-28
    • In the first argument of ‘putStrLn’, namely
        ‘(show ((read num) + 3))’
      In a stmt of a 'do' block: putStrLn (show ((read num) + 3))
      In the expression:
        do putStrLn "Enter an interger: "
           num <- getLine
           putStr "Value + 3 is "
           putStrLn (show ((read num) + 3))
  |
5 |     putStrLn (show((read num) + 3))
  |      
Test.hs:5:15:警告:[-Wtype默认值]
•将以下约束默认为“整型”
(显示a0)因在试验中使用“显示”而产生。hs:5:15-34
(数值a0)因在试验中使用“+”而产生。hs:5:20-33
(读取a0)因在试验中使用“读取”而产生。hs:5:21-28
•在“putStrLn”的第一个参数中,即
“(显示((读取数量)+3))”
在“do”块的stmt中:putStrLn(show((read num)+3))
在表达式中:
输入一个整数:

num
read
具有type
reada=>String->a
,这意味着它可以将
String
转换为属于
read
type类实例的任何类型。它将使用哪种类型取决于周围的代码。在本例中,您将写入
读取num+3
+
运算符也被重载,以用于作为
Num
实例的任何类型,因此
read Num+3
的类型是
(read a,Num a)=>a
。Haskell有一种机制,每当
Num
type类保留在表达式的最终类型中时,它都会选择默认类型。在这种情况下,它默认为
Integer
,因此最后的类型是
Integer

通过启用
-Wtype defaults
警告,您可以看到这一点,该警告也包含在
-Wall
中:

Test.hs:5:15: warning: [-Wtype-defaults]
    • Defaulting the following constraints to type ‘Integer’
        (Show a0) arising from a use of ‘show’ at Test.hs:5:15-34
        (Num a0) arising from a use of ‘+’ at Test.hs:5:20-33
        (Read a0) arising from a use of ‘read’ at Test.hs:5:21-28
    • In the first argument of ‘putStrLn’, namely
        ‘(show ((read num) + 3))’
      In a stmt of a 'do' block: putStrLn (show ((read num) + 3))
      In the expression:
        do putStrLn "Enter an interger: "
           num <- getLine
           putStr "Value + 3 is "
           putStrLn (show ((read num) + 3))
  |
5 |     putStrLn (show((read num) + 3))
  |      
Test.hs:5:15:警告:[-Wtype默认值]
•将以下约束默认为“整型”
(显示a0)因在试验中使用“显示”而产生。hs:5:15-34
(数值a0)因在试验中使用“+”而产生。hs:5:20-33
(读取a0)因在试验中使用“读取”而产生。hs:5:21-28
•在“putStrLn”的第一个参数中,即
“(显示((读取数量)+3))”
在“do”块的stmt中:putStrLn(show((read num)+3))
在表达式中:
输入一个整数:
num在这里您正在使用

show :: Show a => a -> String
read :: Read a => String -> a
(+) :: Num a => a -> a -> a
3 :: Num a => a
在同一类型上
a
。因此,Haskell搜索一个类型
a
满足
Show a,Read a,Num a

原则上,这将被拒绝,因为类型不明确,但Haskell要求对
Num
使用特殊规则,导致
a
被默认,通常为
Integer
。由于这也满足了
Show
Read
,因此程序类型会进行检查。

这里是您正在使用的

show :: Show a => a -> String
read :: Read a => String -> a
(+) :: Num a => a -> a -> a
3 :: Num a => a
在同一类型上
a
。因此,Haskell搜索一个类型
a
满足
Show a,Read a,Num a

原则上,这将被拒绝,因为类型不明确,但Haskell要求对
Num
使用特殊规则,导致
a
被默认,通常为
Integer
。由于这也满足了
Show
Read
,因此程序类型会进行检查。

这里是您正在使用的

show :: Show a => a -> String
read :: Read a => String -> a
(+) :: Num a => a -> a -> a
3 :: Num a => a
在同一类型上
a
。因此,Haskell搜索一个类型
a
满足
Show a,Read a,Num a


原则上,这将被拒绝,因为类型不明确,但Haskell要求对
Num
使用特殊规则,导致
a
被默认,通常为
Integer
。由于这也满足了
Show
Read
,程序类型将进行检查。

IO
不是可以导入的标准模块,您的代码不需要任何导入即可工作。
IO
不是可以导入的标准模块,您的代码不需要任何导入即可工作