我是 Haskell 的新手,谁能建议我如何将 F# 中的以下程序重写为 Haskell,以便尽可能地相似。
不知道如何在 Haskell 中定义堆栈数据类型。
谢谢
let calc input (stck:Stack<double>) =
match input with
| "*" -> stck.Push(stck.Pop() * stck.Pop())
| "+" -> stck.Push(stck.Pop() + stck.Pop())
| "-" -> stck.Push(stck.Pop() - stck.Pop())
| "/" -> stck.Push(stck.Pop() / stck.Pop())
| _ -> stck.Push(System.Convert.ToDouble(input))
let evalu (inputStr:string) =
let stk = Stack<double>()
let elem = inputStr.Split([|' '|])
Array.iter (fun ent -> calc ent stk) elem
stk.Pop()
//Call
Console.WriteLine(evalu("3 5 +"))
想要有类似的东西:
calc input stck
| input == '*' = stck.push(stck.pop * stck.pop)
| input == '+' = stck.push(stck.pop + stck.pop)
| input == '-' = stck.push(stck.pop - stck.pop)
| input == '/' = stck.push(stck.pop / stck.pop)
| otherwise = stck.push(input)
main = calc "3 5 +" [] //Somehow do folding