stackoverflow1453

Programming&&Games&&Life

Archive for F#

Reverse Polish Notation

module TheBookOfFSharp.RpnCalculator
open System
let evalRpnExpr (s : string) =
    let solve items current =
        match (current, items) with
        | "+", y::x::t -> (x + y)::t
        | "-", y::x::t -> (x - y)::t
        | "*", y::x::t -> (x * y)::t
        | "/", y::x::t -> (x / y)::t
        | _ -> (float current)::items
    (s.Split(' ') |> Seq.fold solve []).Head

[<EntryPoint>]
let main argv =
  [ "4 2 5 * + 1 3 2 * + /"
    "5 4 6 + /"
    "10 4 3 + 2 * -"
    "2 3 +"
    "90 34 12 33 55 66 + * - + -"
    "90 3 -" ]
  |> List.map (fun expr -> expr, evalRpnExpr expr)
  |> List.iter (fun (expr, result) -> printfn "(%s) = %A" expr result)
  Console.ReadLine() |> ignore
  0

Printing current time

open System
let today=DateTime.Now
System.Console.WriteLine(today)

1st F# Program

open System
System.Console.WriteLine "Hello World"
Console.ReadLine()