HASKELL *** 异常:Prelude.read:没有解析(可能重复)
HASKELL *** Exception: Prelude.read: no parse (possible duplicate)
我有这个代码
import Data.Char (isDigit)
eval :: [Int] -> IO()
eval liste = do
putStrLn "Please enter a positive integer or an operater ( + / - / * ): "
input <- getLine
let
ord = words input
cmd = read (head ord) :: Char
in
if isDigit cmd then
let nyliste = (read [cmd] :: Int) : liste in do
print nyliste
eval nyliste
else if isOperator cmd then if null liste || length liste == 1 then do
putStrLn "Invalid input! Start by adding at least two positive integers"
eval liste
else let
fst = head liste
snd = head $ tail liste
in case cmd of
'+' -> let
newValue = fst + snd
oppliste = newValue : drop 2 liste
in do
print oppliste
eval oppliste
'-' -> let
newValue = fst - snd
oppliste = newValue : drop 2 liste
in do
print oppliste
eval oppliste
'*' -> let
newValue = fst * snd
oppliste = newValue : drop 2 liste
in do
print oppliste
eval oppliste
_ -> do
putStrLn "Invalid input! Start by adding at least two positive integers"
eval liste
else do
putStrLn "Invalid input! Start by adding at least two positive integers"
eval liste
isOperator :: Char -> Bool
isOperator c = c == '*' || c == '+' || c == '-'
main :: IO()
main = eval []
其中,当我尝试 运行 它时,它给我这个错误:
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, one module loaded.
ghci> main
Please enter a positive integer or an operater ( + / - / * ):
1
*** Exception: Prelude.read: no parse
ghci>
我看过类似的问题,我知道错误与我使用read
有关,但我不明白更多。我在这里做错了什么?
head ord
就是 "1"
。要使 read
成为 Char
,则必须改为 "'1'"
。因为 String
只是一个 [Char]
,你可以只取第一个元素来得到你想要的,而不用在 read
那里费心。
我有这个代码
import Data.Char (isDigit)
eval :: [Int] -> IO()
eval liste = do
putStrLn "Please enter a positive integer or an operater ( + / - / * ): "
input <- getLine
let
ord = words input
cmd = read (head ord) :: Char
in
if isDigit cmd then
let nyliste = (read [cmd] :: Int) : liste in do
print nyliste
eval nyliste
else if isOperator cmd then if null liste || length liste == 1 then do
putStrLn "Invalid input! Start by adding at least two positive integers"
eval liste
else let
fst = head liste
snd = head $ tail liste
in case cmd of
'+' -> let
newValue = fst + snd
oppliste = newValue : drop 2 liste
in do
print oppliste
eval oppliste
'-' -> let
newValue = fst - snd
oppliste = newValue : drop 2 liste
in do
print oppliste
eval oppliste
'*' -> let
newValue = fst * snd
oppliste = newValue : drop 2 liste
in do
print oppliste
eval oppliste
_ -> do
putStrLn "Invalid input! Start by adding at least two positive integers"
eval liste
else do
putStrLn "Invalid input! Start by adding at least two positive integers"
eval liste
isOperator :: Char -> Bool
isOperator c = c == '*' || c == '+' || c == '-'
main :: IO()
main = eval []
其中,当我尝试 运行 它时,它给我这个错误:
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, one module loaded.
ghci> main
Please enter a positive integer or an operater ( + / - / * ):
1
*** Exception: Prelude.read: no parse
ghci>
我看过类似的问题,我知道错误与我使用read
有关,但我不明白更多。我在这里做错了什么?
head ord
就是 "1"
。要使 read
成为 Char
,则必须改为 "'1'"
。因为 String
只是一个 [Char]
,你可以只取第一个元素来得到你想要的,而不用在 read
那里费心。