简体   繁体   中英

Haskell RPN calculator from F#

I am new at Haskell, could anyone suggest me how to rewrite folowing program in F# to Haskell so it will be as much as similar as possible.

Don't know how to define stack data type in Haskell.

Thanks

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 +"))

Would like to have something like:

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

here is a very basic translation:

module Main where

type Stack = [Double]

push :: Stack -> Double -> [Double]
push s v = v:s

pop :: Stack -> (Double, Stack)
pop []    = error "empty stack"
pop (v:s) = (v,s)

calc :: Stack -> String -> Stack
calc stck "+" = let (v1,stck') = pop stck
                    (v2,stck'') = pop stck'
                in push stck'' $ v1+v2
calc stck n = push stck $ read n

eval :: String -> Double
eval = head . foldl calc [] . words

main :: IO ()
main = print $ eval "3 5 +"

as .net's Stack is not pure I opted to use a simple list as an alternative, this of course means that you have to thread the state (the stack) through your calculations (that is the job of those let (v1,stck)... parts)

I think this is the easiest one for an beginner - if you advanced a bit you might come back and reimplement this in using the state-monad (indeed I used it in disguise exactly with the let's) to make it a bit more readable/beautiful

Of course you have to add the other cases for your operators

remarks

This will fail for malformed inputs (as would your version) - if you like you can sprinkle in Maybe (in pop , calc and eval ) ...

making it a bit nicer

if you implement the cases yourself you will find that the let ... pop ... push stuff is repeating a lot - so let's DRY :

apply :: (Double -> Double -> Double) -> Stack -> Stack
apply op stack =
  let (a,stack')  = pop stack
      (b,stack'') = pop stack'
  in push stack'' (b `op` a)

calc :: Stack -> String -> Stack
calc stack "+" = apply (+) stack
calc stack "*" = apply (*) stack
calc stack "-" = apply (-) stack
calc stack "/" = apply (/) stack
calc stack n = push stack $ read n

also note that you have to reverse the operation because you pop in the wrong order

if you don't mind flip ing:

calc :: String -> Stack -> Stack
calc "+" = apply (+)
calc "*" = apply (*)
calc "-" = apply (-)
calc "/" = apply (/)
calc n   = flip push $ read n

eval :: String -> Double
eval = head . foldl (flip calc) [] . words

here is an example:

λ> eval "3 5 + 2 - 3 /"
2.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM