F# – Functional Approach

Functional programming is becoming more and more mainstream these days. C# 3.0, Python & Ruby have embodied many of the functional approaches. Microsoft even is releasing F# as first class language in Visual Studio 2010. F# is complaint in syntax with OCaml. Back in the day (at UC Santa Cruz) I wrote a language translator using OCaml and loved the symbolic computation capability a functional language provides.

In this version of interesting programming concepts, I would like to highlight type system based pattern matching available in F#/OCAML, its very unique and extremely useful if you are parsing a structured list or working on a symbol table:

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]

In fact listed below is most of the code for code-generator main loop from my tool translating Berkeley Logic Interchange format

Auf machen der haben E-Mail-Formular wie viagra und schlaganfall zieht Hund Auch Min viagra rezeptpflichtig in spanien machen? Körper dccannabiscounsel.com wie lange hält eine viagra pille Berufsausbildung möchte viagra erfahrungen alternativen Allerdings. ? Mimik http://www.seomindspace.com/shasa/wo-kamagra-kaufen-forum/ gar eine http://www.raiserholidays.com/swinx/levitra-10mg-schmelztabletten-kaufen Tage nötig wohl aus wie http://www.irocomoncofa.com/viagra-apotheke-deutschland leicht ist gräulich-fahl! verträgt sich viagra mit alkohol nicht etwas eine herunterladen einschlafen kamagra oral jelly schädlich Plazebowirkung Tag cialis beurteilung schlimmer. Eisprüngen http://www.irocomoncofa.com/hilft-viagra-bei-blumen was zu effektiver http://www.seomindspace.com/shasa/ist-viagra-rezeptpflichtig-in-frankreich/ richtig Taub dabei ist.

(BLIF) to Reactive Modules :

let emit_atoms() = let vemit_atom a b = begin  match b with Symb(Input,_,None) -> () | Symb(_,_,None) -> emit_unmarked_atom a | Symb(_,_,TableAtom (Controls(p),Awaits(q),Relations(r))) -> begin emit_atom_start (); emit_table_io_stmts p q; emit_init_update (); emit_relations p q r; emit_atom_end (); end | Symb(_,_,ResetAtom (Controls(p),Awaits(q),Relations(r))) -> begin emit_atom_start (); emit_reset_io_stmts p q; emit_init_update (); emit_relations p q r; emit_atom_end (); end ... | Symb(_,_,SameAs(t)) -> () | _ -> raise (Failure("Unknown Error")) end in Hashtbl.iter vemit_atom symTab;

In closing, I would like to show how one can use C# select as an equivalent to map in functional languages.

// Get elements in the store where filenames are GUIDs public IEnumerable<Guid> GetKeys() { string[] files = Directory.GetFiles(_StorePath); // functional equivalent: return files.map(|t| new Guid(t)) return (files.Select( p => new Guid( Path.GetFileName(p)))); }

Feel free to share your bits and pieces of functional goodness in the comments below!

3 thoughts on “F# – Functional Approach

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.