Visual F# 100 Examples: Example Number 7

So far I have been sharing to you guys some examples of Window Based application in Visual F#. This time, for variation sake lets make some console application:

Problem: Make a simple console application mind reader game.
open System
//change the title to Simple Mind Reader Game
System.Console.Title<-"Simple Mind Reader Game"
//change the foreground color to cyan
Console.ForegroundColor<-ConsoleColor.Cyan
//dislay the game title screen
printfn "\t\t\t Mind Reader Game"
printfn "\t\t\tPress enter to continue..."
//waits for a keypress from the user
System.Console.ReadLine()|>ignore
System.Console.Clear()
//the following does the game action sequence
printfn "\t\t\t Think of a five-digit number that has five unique numbers(Eg. 12345)"
printfn "\t\t\tPress enter to continue..."
System.Console.ReadLine()|>ignore
System.Console.Clear()

printfn "\t\t\t Reverse the numbers then subtract the smaller number from the larger number(Eg.54321- 12345)"
printfn "\t\t\tPress enter to continue..."
System.Console.ReadLine()|>ignore
System.Console.Clear()

printfn "\t\t\t Add 10000 to the difference"
printfn "\t\t\t Press enter to continue..."
System.Console.ReadLine()|>ignore
System.Console.Clear()

printfn "\t\t\tVisualize the sum while I try to read your mind."
printfn "\t\t\tPress enter to continue..."
System.Console.ReadLine()|>ignore
System.Console.Clear()

printfn "\t\t\tThe sum is…"
printfn "\t\t\tPress enter to continue..."
System.Console.ReadLine()|>ignore
System.Console.Clear()

printfn "\t\t\tThe sum is…"
printfn "\t\t\tPress enter to continue..."
System.Console.ReadLine()|>ignore
System.Console.Clear()

printfn "\t\t\tThe sum is…"
printfn "\t\t\tPress enter to continue..."
System.Console.ReadLine()|>ignore
System.Console.Clear()

printfn "\t\t\t51976"

Visual F# 100 Examples: Example Number 6

Problem. Create an application that will display the surface area of a cube. The surface area of a cube can be computed by multiplying the inputted side by itself and multiplying the product by 6.
// Learn more about F# at http://fsharp.net
//specifies the memory location of the class files
//that will be needed in our application
open System.Collections.Generic
open System
open System.Windows.Forms
open System.ComponentModel
open System.Drawing
//creates a new form
let cubeform=new Form(Text="Compute the Surface Area of a Cube", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates our controls
let n1label=new Label(Text="Enter a side value:",Location=new System.Drawing.Point(0,20),AutoSize=true)
let firsttextbox=new TextBox(Location=new System.Drawing.Point(120, 20))
let n2label=new Label(Text="Surface area of a cube:", Location=new System.Drawing.Point(0, 90),AutoSize=true)
//creates a label that will display the result of the computation
let anslabel=new Label(Location=new System.Drawing.Point(140, 90), BorderStyle=BorderStyle.FixedSingle)
//make our buttons
let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 130))
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130))
//add the controls into the form
cubeform.Controls.Add(n1label)
cubeform.Controls.Add(firsttextbox)
cubeform.Controls.Add(n2label)
cubeform.Controls.Add(anslabel)
cubeform.Controls.Add(computebutton)
cubeform.Controls.Add(exitbutton)

//when the compute button is clicked
computebutton.Click.Add(fun ans->
let sidevalue=Convert.ToDouble(firsttextbox.Text)
let areaofacube=6.00*(sidevalue*sidevalue)

//display the areaofacube value in the anslabel
anslabel.Text<-Convert.ToString(areaofacube))
 //when the exit button is clicked, close the form            
exitbutton.Click.Add(fun exit -> cubeform.Close())  
Application.Run(cubeform)

We'll be adding some new controls after our tenth example:)