

Tell HN: F# Rocks - shawndumas

<p><pre><code>  type Expr = 
    | Num of int
    | Add of Expr * Expr
    | Mul of Expr * Expr
    | Var of string
 
  let rec Evaluate (env:Map&#60;string,int&#62;) exp = 
  	  match exp with
  	  | Num n -&#62; n
	  | Add (x,y) -&#62; Evaluate env x + Evaluate env y
	  | Mul (x,y) -&#62; Evaluate env x * Evaluate env y
	  | Var id    -&#62; 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
</code></pre>
=&#62; 5
======
Khao
Could you care to explain? Right now what I understand is not "F# Rocks" but
"F# is the most weird language I've ever seen and no one cares to explain me
why I should consider F# as something that rocks" :(

~~~
grayrest
It's a simple expression evaluator evaluating a + 2 * b with a = 1 and b = 2.
The fun part is that it's all done using only the one recursive Evaluate
function with everything else being driven by the type system and pattern
matching. The entire program is also strongly typed with only one type
declaration (the env argument to Evaluate).

F# is a neat language. I'm sort of surprised he went with the type system
rather than computation expressions/async workflows, which are my favorite
feature. They aren't unique (they're monads) but the syntax is nice. I'd
happily code in F# if I had a reason to write .NET code.

