Phil Trelford's Array
POKE 36879, 255

F# Eye for the VB Guy

February 25, 2014 23:35 by phil

Are you a VB developer curious about functional-first programming. F# is a statically typed language built into Visual Studio. It is a multi-paradigm language with both functional and object-oriented constructs.

F# has powerful type inference which reduces the amount of typing you need to do without reducing performance or correctness.

F# projects are easily referenced from VB and vice versa. Like VB, F# makes minimal use of curly braces, and for many operations the syntax will feel quite familiar.

Here’s my cut-out-and-keep guide to common operations in both languages:

Declaring values

VB.Net F#
' Fully qualified
Dim greeting As String = "Hello"
' Type inferred
Dim greeting = "Hello"
// Fully qualified 
let greeting : string = "Hello"
// Type inferred 
let greeting = "Hello"

 

Declaring functions

VB.Net F#
Sub Print(message As String)
  Console.WriteLine(message)
End Sub

Function Add _
  (a As Integer, b As Integer) _
  As Integer
  Return a + b
End Function
let print( message: string ) = 
  Console.WriteLine(message) 

// Return type is inferred
let add(a:int, b:int) = a + b

 

Loops

VB.Net F#
For i = 1 To 10
  Console.WriteLine("Hello")
Next

For Each c In "Hello"
  Console.WriteLine(c)
Next
for i = 1 to 10 do 
   Console.WriteLine("Hello") 


for c in "Hello" do 
  Console.WriteLine(c)

 

If/Then/Else

VB.Net F#
Dim ageGroup As String
If age < 18 Then
  ageGroup = "Junior"
Else
  ageGroup = "Senior"
End If
let ageGroup = 
  if age < 18 then  
    "Junior"
  else  
    "Senior"
 

 

Pattern Matching

VB.Net F#
' Score Scrabble letter
Select Case
c Case "A", "E", "I", "L", "N", _ "O", "R", "S", "T", "U" Return 1 Case "D", "G" Return 2 Case "B", "C", "M", "P" Return 3 Case "F", "H", "V", "W", "Y" Return 4 Case "K" Return 5 Case "J", "X" Return 8 Case "Q", "Z" Return 10 Case Else Throw New _
      InvalidOperationException()
  End Select
// Score scrabble letter
match letter with
| 'A' | 'E' | 'I' | 'L' | 'N' 
| 'O' | 'R' | 'S' | 'T' | 'U' -> 1
| 'D' | 'G' -> 2
| 'B' | 'C' | 'M' | 'P' -> 3
| 'F' | 'H' | 'V' | 'W' | 'Y' -> 4
| 'K' -> 5
| 'J' | 'X' -> 8
| 'Q' | 'Z' -> 10
| _ -> invalidOp ""

 

Try/Catch

VB.Net F#
Dim i As Integer = 5
Try
  Throw New ArgumentException()
Catch e As OverflowException _
      When i = 5
  Console.WriteLine("First handler")
Catch e As ArgumentException _
      When i = 4
  Console.WriteLine("Second handler")
Catch When i = 5
  Console.WriteLine("Third handler")
End Try
let i = 5
try
  raise (ArgumentException())
with
| :? OverflowException when i = 5 ->
  Console.WriteLine("First handler")
| :? ArgumentException when i = 4 ->
  Console.WriteLine("Second handler")
| _ when i = 5 ->
  Console.WriteLine("Third handler")

 

Modules

VB.Net F#
Module Math
  ' Raise to integer power
  Function Pown( _
    x As Double, y As Integer)
    Dim result = 1
    For i = 1 To y
      result = result * x
    Next
    Return result
  End Function
End Module
module Maths =
  // Raise to integer power
 let pown (x:float,y:int) =
   let mutable result = 1.0
   for i = 1 to y do
     result <- result * x
   result

 

Classes

VB.Net F#
' Immutable class
Public Class Person
  Private ReadOnly myName As String

  Public Sub New(name As String)
    myName = name
  End Sub

  ReadOnly Property Name() As String
    Get
      Return myName
    End Get
  End Property
End Class

' Inheritance
Public Class MyWindow _
  : Inherits Window
End Class
// Immutable class
type Person (name : string) = 
  member my.Name = name

// Inheritance
type MyWindow() =
   inherit Window()

 

Summary

Interested in learning more? Give F# a try in Visual Studio with the built-in F# Tutorial project or in your browser with http://tryfsharp.org


Tags:
Categories: Basic | F#
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Comments

March 7. 2014 02:42

nicolas

The similarity is striking : it is really the same, without the noise.

So on one hand you can do everything you are used to in quite a direct way.
But at the same time, if you want to use more advanced constructs, and only if you do, you can.

And those advanced perks, from ML languages tradition have decades of smartness backed in, so you can bet they are powerful...

Nicolas

nicolas

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading