Saturday, October 16, 2010

F# - Basics by Example



Continue with the Basics by Example; today's version of the post written in F# Enjoy!

I was thinking in changing the name of this post to "F# - OO Basics by Example" because I guess its confusing the fact that F# is more a Functional Programming language than an Imperative one, even if both paradigms are well supported by the language. At the end I decided to leave it like that, I just want to make clear that you will not find any "Functional Basics" on this post ;) ... I will probably do the same in my next Scala post.

You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 



Greetings Program - Verbose
// F# Basics
namespace GreetProgram  
open System  
  
// Constructor
type public Greet(message:string, name:string, loopMessage:int) = class  
    // Attributes or Fields
    let mutable message:string = message
    let mutable name:string = name
    let mutable loopMessage:int = loopMessage
    // Properties
    member public this.Name 
        with get() = name 
        and set(value:string) = name <- this.Capitalize(value)
    member public this.Message 
        with get() = message 
        and set(value:string) = message <- this.Capitalize(value)
    member public this.LoopMessage 
        with get() = loopMessage
        and set(value:int) = loopMessage <- value
    // Overloaded Constructor
    new() = Greet("", "", 0)
    // Method 1
    member private self.Capitalize(value:string) = (
        // "if-then-else" statement 
        if value.Length >= 1 then 
            value.[0].ToString().ToUpper() + value.[1..]            
        else
            ""
    )
    // Method 2  
    member public self.Salute() = (
        // "for" statement
        for i = 1 to self.LoopMessage do            
            printfn "%s %s!" self.Message self.Name            
    )
    // Overloaded Method 2.1
    member public self.Salute(message:string, name:string, loopMessage:int) = (
        // "while" statement
        let mutable i:int = 0
        while i < loopMessage do
            printfn "%s %s!" (self.Capitalize message) (self.Capitalize name)
            i <- i + 1
    )
    // Overloaded Method 2.2
    member public self.Salute(name:string) = (
        // "switch/case" statement is not supported  
        // using match statement instead
        let dtNow:DateTime = DateTime.Now   
        let t:int = dtNow.Hour
        let timeMessage =
            match t with
            |6|7|8|9|10|11 -> self.Message <- "good morning,"  
            |12|13|14|15|16|17 -> self.Message <- "good afternoon,"  
            |18|19|20|21|22 -> self.Message <- "good evening,"  
            |23|0|1|2|3|4|5 -> self.Message <- "good night,"  
            | _ -> self.Message <- "huh?"
        printfn "%s %s!" (self.Capitalize self.Message) (self.Capitalize name)
    )
end  
  
// Console Program
module public GreetProgram = 
    // Define object of type Greet  
    let g:Greet = new Greet()
    // Instantiate Greet. Call Constructor 
    // Call Set Properties  
    g.Message <- "hello"
    g.Name <- "world"
    g.LoopMessage <- 5
    // Call Method 2  
    g.Salute()
    // Call Overloaded Method 2.1 and Get Properties
    g.Salute(g.Message, "f#", g.LoopMessage)
    // Call Overloaded Method 2.2 
    g.Salute("carlos")

    // Stop and Exit
    printfn "Press any key to exit..."
    Console.Read() |> ignore

Greetings Program - Minimal
// F# Basics
open System  
  
// Constructor
type Greet(message:string, name:string, loopMessage:int) = 
    // Attributes or Fields 
    let mutable message:string = message
    let mutable name:string = name
    let mutable loopMessage:int = loopMessage
    // Properties
    member this.Name 
        with get() = name 
        and set(value:string) = name <- this.Capitalize(value)
    member this.Message 
        with get() = message 
        and set(value:string) = message <- this.Capitalize(value)
    member this.LoopMessage 
        with get() = loopMessage 
        and set(value:int) = loopMessage <- value
    // Overloaded Constructor
    new() = Greet("", "", 0)
    // Method 1
    member private self.Capitalize(value:string) = 
        // "if-then-else" statement 
        if value.Length >= 1 then 
            value.[0].ToString().ToUpper() + value.[1..]
        else
            ""
    // Method 2  
    member self.Salute() =
        // "for" statement
        for i = 1 to self.LoopMessage do            
            printfn "%s %s!" self.Message self.Name
    // Overloaded Method 2.1
    member self.Salute(message:string, name:string, loopMessage:int) = 
        // "while" statement
        let mutable i:int = 0
        while i < loopMessage do
            printfn "%s %s!" (self.Capitalize message) (self.Capitalize name)
            i <- i + 1
    // Overloaded Method 2.2
    member self.Salute(name:string) = 
        // "switch/case" statement is not supported  
        // using match statement instead
        let dtNow = DateTime.Now        
        let t = dtNow.Hour
        let timeMessage =
            match t with
            |6|7|8|9|10|11 -> self.Message <- "good morning,"  
            |12|13|14|15|16|17 -> self.Message <- "good afternoon,"  
            |18|19|20|21|22 -> self.Message <- "good evening,"  
            |23|0|1|2|3|4|5 -> self.Message <- "good night,"  
            | _ -> self.Message <- "huh?"        
        printfn "%s %s!" (self.Capitalize self.Message) (self.Capitalize name)
  
// Console Program
// Define object of type Greet  
let g = Greet()
// Instantiate Greet. Call Constructor 
// Call Set Properties  
g.Message <- "hello"
g.Name <- "world"
g.LoopMessage <- 5
// Call Method 2  
g.Salute()
// Call Overloaded Method 2.1 and Get Properties
g.Salute(g.Message, "f#", g.LoopMessage)
// Call Overloaded Method 2.2 
g.Salute("carlos")

// Stop and Exit
printfn "Press any key to exit..."
Console.Read() |> ignore


And the Output is:


3 comments:

  1. Hey Carlos!

    In F# the "new" keyword is optional!
    That means that you need to remove it from the "Greetings Program - Minimal"

    Current: let g = new Greet()
    Should be: let g = Greet()

    You should also update the F# number of keywords used in the "How many keywords you write in your code Part 2" post.

    ReplyDelete
  2. Thank you for your/my comment!
    I will do it as soon as possible :)

    ReplyDelete