Beginning F# – Console app and a Form

Just learning some F# basics – the following the tale of my journeys.

( This is literally going to be like a set of -superawesome- notes )

wat

*Equips Visual Studio*

*Equips PluralSight*

Ready.

Starting off with a F# Console Application

printfn

in a console application this will print text to the console.

11182014_printfn

intelligence+1

let

creates  an -immutable- binding to a value. Since let binding are immutable by default, what is normally referred to as a variable is now referred to as a value (I’m going to assume its ok to keep calling it a variable as long as you know that it’s supposed to be immutable in F#). Immutable means that once an object has been created, it cannot be modified. Its like a Paladin bubble, except instead of repelling attacks for a small duration it repels definition changes for-ev-er. But… You can make a value mutable by simply sticking the keyword mutable after let.

11182014_immutableVSmutable

11182014_let

to print a value in a printfn string, you must use a special character %s (%d for an int)

11182014_printfnWithVariable

intelligence+1

open

allows you to ‘open’ a namespace to use; functional equivalent of a using statement in C#

11182014_open

opening System.Windows.Forms requires adding a reference to the project.

intelligence+1

type

allows you to create a data type.

11182014_type

which can be used to store data based upon the provided constructor (what’s in the curly braces)

11182014_type_instantiatingTheType

F# already knows that you’re creating a new value of type Person based upon the parameters provided.

Intelligence + 2

Array [| |]

an array can be created by using a ‘[|’ to open the array and ‘|]’ to close it

11182014_array

I created an array of Person types that referenced use previously created data like my phil Person and the data read from the user’s input.

Intelligence + 1

forms

time to make use of the open statement from above. Forms allow you to create a graphical representation of your data.

11182014_forms

F# uses .NET just as C# can, to create a new form, use the ‘new’ keyword and instantiate a new Form().

The form requires a control to present the data, else it’s just a empty form. Instantiate a new GridGridView() with the ‘new’ keyword. Once it’s been created, it needs to be added to the form.

Finally, Application.Run(form) will let the app know to open up the form.

Here’s the finished product of this learning experience:

11182014_runApplication

When the array is presented in the form, it was nice enough to sort the data out instead of having it all willy-nilly the way I entered it.

Intelligence = Over 9000.

2012-02-14-hold all this slinky

I was in a really weird mood while writing this blog. Hopefully if you read it, you got some enjoyment out of it. I learned something, maybe you did too ;D

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s