Haskell 如何更改函数签名
我是Haskell的初学者,现在正在尝试开发后缀计算器。 以下是我目前的代码:Haskell 如何更改函数签名,haskell,Haskell,我是Haskell的初学者,现在正在尝试开发后缀计算器。 以下是我目前的代码: calcK :: String -> String calcK = show calc calc :: String -> [Double] calc = foldl interprete [] . words interprete s x | x `elem` ["+","-","*","/","^"] = operate x s | x `elem` ["inc","dec","sqrt
calcK :: String -> String
calcK = show calc
calc :: String -> [Double]
calc = foldl interprete [] . words
interprete s x
| x `elem` ["+","-","*","/","^"] = operate x s
| x `elem` ["inc","dec","sqrt","sin","cos","inv"] = operate2 x s
| x `elem` ["+all"] = [sum s]
| x `elem` ["*all"] = [product s]
| otherwise = read x:s
where
operate op (x:y:s) = case op of
"+" -> x + y:s
"-" -> y - x:s
"*" -> x * y:s
"/" -> y / x:s
"^" -> y ** x:s
operate2 op (x:s) = case op of
"inc" ->1 + x:s
"dec" -> (-1) + x:s
"sqrt" -> sqrt x:s
"sin" -> sin x:s
"cos" -> cos x:s
"inv" -> 1 / x:s
它只适用于基本操作。然而,我意识到我需要将签名定义为
String -> String
非字符串->[Double]
。因此,“12+”
的计算结果应为
"[3.0]"
而不是
[3.0]
正如你所看到的,我试图在顶部做一个show
,但它似乎不起作用。我曾尝试将show
添加到代码的其他位置,但仍然没有成功
如果专家们能就此提供一些建议,我将不胜感激。提前谢谢 唯一的问题是您试图显示
calc
函数,而不是其结果
你想编写
show
和calc
,让calc
生成一个[Double]
,然后传递给show
,特别是对于初学者,我认为值得注意的是,这与calcK x=show(calc x)
是一样的。对,我没有看到这一点。
calcK :: String -> String
calcK = show . calc