我遇到了问题而且我已经有一段时间了。我有一个字符串表,目的是计算一系列Int值,并使用前缀运算符作为输入和数字输入。
想法是在控制台中写入“+ 3 * 2 4”并让它计算3 +(2 * 4),然后输出答案。我对Haskell很新,我很感激一些帮助。感谢。
我已经制作了一些代码来解析字符串作为字符串表,每个数字和运算符作为字符串表中的单个元素导入Data.Char
import Data.Char
data AST = Leaf Int | Sum AST AST | Min AST | Mult AST AST
parseCharacter :: String -> [String]
parseCharacter [] = []
parseCharacter (' ':xs) = parseCharacter xs
parseCharacter ('(':xs) = "(": parseCharacter xs
parseCharacter (')':xs) = ")": parseCharacter xs
parseCharacter ('+':xs) = "+": parseCharacter xs
parseCharacter ('-':xs) = "-": parseCharacter xs
parseCharacter ('*':xs) = "*": parseCharacter xs
parseCharacter (x:xs) = if isDigit x then (takeWhile isDigit (x:xs)) : parseCharacter (dropWhile isDigit xs) else parseCharacter xs