没有注释的 Haskell 读取函数

蓝山咖啡

haskell 读取函数是否将数据类型隐式转换为另一种数据类型?

import IO

main = do
    putStrLn "Enter an interger: "
    num <- getLine
    putStr "Value + 3 is "
    putStrLn (show((read num) + 3))

那么,haskell 解释器会自动将 num 的数据类型从 string 更改为 int 以处理 '+ 3' 吗?

恶梦

read有 type Read a => String -> a,这意味着它可以将 aString转换为作为Read类型类实例的任何类型它将使用哪种类型取决于周围的代码。在这种情况下,您编写read num + 3. +运营商还重载工作对任何类型是一个实例Num,所以类型read num + 3(Read a, Num a) => aHaskell 有一种机制,只要Num类型类保留在表达式的最终类型中,就会选择默认类型在这种情况下,它默认为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))
  |      

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

没有参数的Haskell多元函数

递归Haskell函数似乎没有终止

函数中的Haskell类型注释

在python中读取没有注释的文件

没有注释的Guice构造函数注入?

为什么在Haskell中没有runConst函数?

为什么Haskell的main函数没有任何参数?

没有@Inject构造函数或@Provides注释方法无法提供

Hibernate注释:实体没有默认构造函数

有没有一种方法可以捕获Haskell中的高阶函数元组?

有没有办法在Haskell中转换函数类型?

有没有更好的方法来编写这个 haskell 函数?

有没有办法在 Haskell 的函数内打印变量?

文本有Haskell readMaybe函数吗?

具有不同数量参数的Haskell函数

Haskell中读取函数的大小写匹配

Spring依赖项注入如何在带有@Component注释但没有构造函数注释的类中工作?

为什么Scala没有为每个monad定义返回/单位函数(与Haskell相反)?

在haskell循环异常中,其函数体中没有循环

模式匹配(es)对于没有 `otherwise` 子句的 Haskell 函数来说并不详尽

使用反射读取Kotlin函数注释值?

Haskell:有没有一种方法可以从函数内部推断出函数的返回类型?

在Haskell中,您能否拥有一个调用其他函数但没有实现的函数的签名?

没有++的haskell中的ConcatMap

F#中带有类型注释的值限制,而没有显式函数参数

在React函数中无法正确读取输入(没有JSX)

使用从文件读取的结构的函数没有输出吗?

WorksheetFunction.Countif 行没有读取我的地址函数

任何以注释方式在没有Spring的情况下读取属性文件的API