<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>UK StudentZine Blog : F#</title><link>http://blogs.msdn.com/ukstudentzine/archive/tags/F_2300_/default.aspx</link><description>Tags: F#</description><dc:language>en-GB</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>F# ...Functional programming in the .Net world - Kevin Pfister</title><link>http://blogs.msdn.com/ukstudentzine/archive/2009/02/17/f-functional-programming-in-the-net-world-kevin-pfister.aspx</link><pubDate>Tue, 17 Feb 2009 17:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9427633</guid><dc:creator>ukstudentzine</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/ukstudentzine/comments/9427633.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ukstudentzine/commentrss.aspx?PostID=9427633</wfw:commentRss><description>&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;F# was developed as an experimental programming language by Microsoft Research to provide a combination of many sought after features in other programming languages.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;The problem with most programming languages is that they usually have to give up on functionality to achieve it elsewhere; C# for example gave up on a slight performance overhead while gaining the benefits of managed code.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;The goal for F# was to create a type-inferred functional programming language that provides type safety, succinctness, expressivity, scripting while still maintaining performance relative to a well-supported modern runtime system.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;F# is a fully supported .NET language and you can download the CTP from the &lt;/FONT&gt;&lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyID=61ad6924-93ad-48dc-8c67-60f7e7803d3c&amp;amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?FamilyID=61ad6924-93ad-48dc-8c67-60f7e7803d3c&amp;amp;displaylang=en"&gt;&lt;FONT size=3 face=Calibri&gt;Microsoft download site&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=3 face=Calibri&gt;. Developed as a pragmatically oriented variant of ML with a core that shares many of the language fundamentals as OCaml, F# can still run near the speed of C++.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;The first port of call for anyone wanting to know more about F# would be its &lt;/FONT&gt;&lt;A href="http://msdn.microsoft.com/en-us/fsharp/default.aspx" mce_href="http://msdn.microsoft.com/en-us/fsharp/default.aspx"&gt;&lt;FONT size=3 face=Calibri&gt;developer section&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=3 face=Calibri&gt; on MSDN, I’m now going to go through and explain more technically about F# and how to program in it.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Once you have downloaded and installed the CTP for F# you can now go into visual studio and create a new project in the language.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 10pt" class=MsoNormal align=center&gt;&lt;SPAN style="mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;&lt;?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /&gt;&lt;v:shapetype id=_x0000_t75 stroked="f" o:preferrelative="t" o:spt="75" filled="f" path="m@4@5l@4@11@9@11@9@5xe" coordsize="21600,21600"&gt;&lt;v:stroke joinstyle="miter"&gt;&lt;/v:stroke&gt;&lt;v:formulas&gt;&lt;v:f eqn="if lineDrawn pixelLineWidth 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 1 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum 0 0 @1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @2 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 0 1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @6 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @8 21600 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @10 21600 0"&gt;&lt;/v:f&gt;&lt;/v:formulas&gt;&lt;v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"&gt;&lt;/v:path&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:lock aspectratio="t" v:ext="edit"&gt;&lt;/o:lock&gt;&lt;/v:shapetype&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;&amp;nbsp;&lt;IMG style="WIDTH: 668px; HEIGHT: 556px" title=FSharp alt=FSharp src="http://www.microsoft.com/library/media/2057/uk/academia/images/studentzine/FSharp2.png" width=668 height=556 mce_src="http://www.microsoft.com/library/media/2057/uk/academia/images/studentzine/FSharp2.png"&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;As F# is based on OCaml it means it has the ability to cross compile OCaml source code, however this also means it can create a syntax that isn’t as elegant as we would hope for, thus we can use the &lt;B style="mso-bidi-font-weight: normal"&gt;#light&lt;/B&gt; directive at the being of our source to simplify the syntax.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 10pt" class=MsoNormal align=center&gt;&lt;SPAN style="mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 10pt" class=MsoNormal align=center&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&lt;IMG style="WIDTH: 346px; HEIGHT: 256px" title=FSharp alt=FSharp src="http://www.microsoft.com/library/media/2057/uk/academia/images/studentzine/FSharp3.png" width=346 height=256 mce_src="http://www.microsoft.com/library/media/2057/uk/academia/images/studentzine/FSharp3.png"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;The next big thing about F# is Type Inference, if we were to make a function in C# we would have to say what the argument types were and what the return type was, F# however makes these determinations at the compiler stage using the function signature. If we were to make the following declaration of a function in F# that doubles a number given to it.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;let doublenum x = x * 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;This would cause the F# to believe the return value would be an int... why? Well the return value is the last executable statement, in this case ‘x * 2’ F# also knows that there is only 1 argument to the function ‘x’. The reason however why it will return an int is because the last statement contains *, although * can be applied to byte, uint64, double etc. F# will default it to a signed 32bit integer for our convenience.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Now we can override the way that F# does this to make some interesting effects by applying type annotation to one or more arguments. We can do this by declaring what type they are, for example.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;let concat (x: string) y = x + y&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;We have stated that x is of type string, this then infers that y must also be a string as + only works when both variables are strings.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Now what if we want to return more than one result? This can be achieved by wrapping the values you want to return in brackets at the end of the function, as shown in the following example.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;let&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; wordCount text =&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;let&lt;/SPAN&gt; words = String.split [&lt;SPAN style="COLOR: maroon"&gt;' '&lt;/SPAN&gt;] text&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;let&lt;/SPAN&gt; wordSet = Set.of_list words&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;let&lt;/SPAN&gt; nWords = words.Length&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;let&lt;/SPAN&gt; nDups = words.Length - wordSet.Count&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;(nWords,nDups)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;This function returns the number of words in text (nWords) and the number of duplicate words (nDups).&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;So now you know how to make functions in F#, let’s move into GUI. As F# utilises .Net we can use &lt;B style="mso-bidi-font-weight: normal"&gt;System.Windows.Forms&lt;/B&gt; and from that we can make Windows Forms to display things on. Below shows how easy it is to make a new window in F#. ( Remember you have to reference System.Windows.Forms as part of the project as well)&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;open&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; System.Windows.Forms;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;let&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; form = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; Form(Visible=&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;,TopMost=&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;,Text=&lt;SPAN style="COLOR: maroon"&gt;"Hello GUI World"&lt;/SPAN&gt;);;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Now if you want to add a control to the form you have to add it to the Forms Control property, below is an example of adding a RichTextBox to the form we just made.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;let&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; textB = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; RichTextBox(Dock=DockStyle.Fill, Text=&lt;SPAN style="COLOR: maroon"&gt;"Hello GUI World"&lt;/SPAN&gt;);;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;form.Controls.Add(textB);;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;So in all F# is a very powerful and interesting language which is great if you are moving from the functional world of ML, OCaml etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;If you want to try out some more samples you can download &lt;/FONT&gt;&lt;A href="http://code.msdn.microsoft.com/fsharpsamples" mce_href="http://code.msdn.microsoft.com/fsharpsamples"&gt;&lt;FONT size=3 face=Calibri&gt;F# samples&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=3 face=Calibri&gt; at code.msdn which shows a selection of slightly more complex examples for those wanting a challenge.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9427633" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ukstudentzine/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/ukstudentzine/archive/tags/F_2300_/default.aspx">F#</category></item></channel></rss>