Discussion

Tell HN: F# Rocks

  type Expr = 
    | Num of int
    | Add of Expr * Expr
    | Mul of Expr * Expr
    | Var of string
 
  let rec Evaluate (env:Map<string,int>) exp = 
  	  match exp with
  	  | Num n -> n
	  | Add (x,y) -> Evaluate env x + Evaluate env y
	  | Mul (x,y) -> Evaluate env x * Evaluate env y
	  | Var id    -> env.[id]

  let envA = Map.ofList [ "a", 1; "b", 2; "c", 3 ]

  let expT1 = Add(Var "a",Mul(Num 2,Var "b"))
  let resT1 = Evaluate envA expT1
=> 5
8 points shawndumas 15 years ago 5 comments

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton