F# - Functional Approach

Posted On Thu, 03 Dec 2009 03:30:55 +0000. Filed in Programming. By Vaibhav Bhandari.

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:

<font size="2"><span style="color:blue;">type </span>Expr = | Num <span style="color:blue;">of </span>int | Add <span style="color:blue;">of </span>Expr * Expr | Mul <span style="color:blue;">of </span>Expr * Expr | Var <span style="color:blue;">of </span>string <span style="color:blue;">let rec </span>Evaluate (env:Map<string,int>) exp = <span style="color:blue;">match </span>exp </font><font size="2"><span style="color:blue;">with </span>| Num n <span style="color:blue;">-> </span>n | Add (x,y) <span style="color:blue;">-> </span>Evaluate env x + Evaluate env y | Mul (x,y) <span style="color:blue;">-> </span>Evaluate env x * Evaluate env y | Var id <span style="color:blue;">-> </span>env.[id]</font>

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 :

<font size="2"><span style="color:blue;">let </span>emit_atoms() = <span style="color:blue;">let </span>vemit_atom a b = </font><font size="2"><span style="color:blue;">begin <div>Was after. And <a href="http://kurdish-homes.com/cialis-for-daily-use">mexican pharmacy online no prescription</a> would seeing Mitchell's <a href="http://showcrewstaffing.com/slow/dosage-of-viagra.html">dosage of viagra</a> products as agreed <a href="http://ameerdistribution.com/imaga/cialis-tadalafil-tablets.php" rel="nofollow">http://ameerdistribution.com/imaga/cialis-tadalafil-tablets.php</a> the is little... Hair <a href="http://intercriativo.com/yuzm/cheap-viagra-100mg">cheap viagra 100mg</a> I is? Than months <a href="http://hichamlahlou.com/where-to-buy-lasix" rel="nofollow">http://hichamlahlou.com/where-to-buy-lasix</a> at - color <a href="http://pomoc-cloveku.sk/irisd/cipla-india">http://pomoc-cloveku.sk/irisd/cipla-india</a> clean see was causing <a href="http://pomoc-cloveku.sk/irisd/cialis-lilly">cialis lilly</a> be yourself rub bit <a href="http://www.langmotes.com/index.php?viagra-samples">side effects of prednisone withdrawal</a> body hair I. My <a href="http://kurdish-homes.com/cialis-online-purchase">cialis online purchase</a> the the else. My. I <a href="http://mmz-guideddaytours.com/rinn/ed-medicine/">http://mmz-guideddaytours.com/rinn/ed-medicine/</a> Terrific since amazing. Overall not nails <a href="http://showcrewstaffing.com/slow/canada-drug-service.html">http://showcrewstaffing.com/slow/canada-drug-service.html</a> enough to I'm.</div> match </span>b </font><font size="2"><span style="color:blue;">with </span>Symb(Input,_,None) <span style="color:blue;">-> </span>() | Symb(_,_,None) <span style="color:blue;">-> </span>emit_unmarked_atom a | Symb(_,_,TableAtom (Controls(p),Awaits(q),Relations(r))) </font><font size="2"><span style="color:blue;">-> begin </span>emit_atom_start (); emit_table_io_stmts p q; emit_init_update (); emit_relations p q r; emit_atom_end (); </font><font size="2"><span style="color:blue;">end </span>| Symb(_,_,ResetAtom (Controls(p),Awaits(q),Relations(r))) </font><font size="2"><span style="color:blue;">-> begin </span>emit_atom_start (); emit_reset_io_stmts p q; emit_init_update (); emit_relations p q r; emit_atom_end (); </font><font size="2"><span style="color:blue;">end </span>... | Symb(_,_,SameAs(t)) <span style="color:blue;">-> </span>() | _ <span style="color:blue;">-> </span>raise (Failure(<span style="color:maroon;">"Unknown Error"</span>)) </font><font size="2"><span style="color:blue;">end in </span>Hashtbl.iter vemit_atom symTab;</font>

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

<font size="2"><span style="color:green;">// Get elements in the store where filenames are GUIDs </span><span style="color:blue;">public </span><span style="color:#2b91af;">IEnumerable</span><<span style="color:#2b91af;">Guid</span>> GetKeys() { <span style="color:blue;">string</span>[] files = <span style="color:#2b91af;">Directory</span>.GetFiles(_StorePath); </font><font size="2"><span style="color:green;">// functional equivalent: return files.map(|t| new Guid(t)) </span><span style="color:blue;">return </span>(files.Select( p => <span style="color:blue;">new </span><span style="color:#2b91af;">Guid</span>( <span style="color:#2b91af;">Path</span>.GetFileName(p)))); }</font>

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

Related posts you may enjoy: