String 将字符串转换为数字并打印到命令行
我正在练习第1章的练习。对于问题2,我想使用String 将字符串转换为数字并打印到命令行,string,haskell,ghc,String,Haskell,Ghc,我正在练习第1章的练习。对于问题2,我想使用read函数将字符串转换为数字,但下面的代码不起作用 main = do args <- getArgs myNum <- read $ args !! 0 putStrLn myNum main=do args这里有几件事不对 首先,要在此函数中保存变量,需要使用let variable=“something”语句类型,而不是 ex2.hs:7:12: No instance for (Read (IO
read
函数将字符串转换为数字,但下面的代码不起作用
main = do
args <- getArgs
myNum <- read $ args !! 0
putStrLn myNum
main=do
args这里有几件事不对
首先,要在此函数中保存变量,需要使用let variable=“something”
语句类型,而不是
ex2.hs:7:12:
No instance for (Read (IO t0)) arising from a use of ‘read’
In the expression: read
In a stmt of a 'do' block: one <- read $ (args !! 0)
In the expression:
do { args <- getArgs;
myNum <- read $ (args !! 0);
putStrLn myNum }
main = do
-- get command line arguments
args <- getArgs
-- get the first indexed element; convert it from string to float
let myNum = read (args !! 0) :: Float
-- print this number to the command line (as a string)
putStrLn (show myNum)