Sample
Object Oriented
Simple Class with Default Constructor
Simple Class with Constructor Parameters
Class with Multiple Constructors
Inherit an Interface from another Interface
Anonymously Implement an Interface
Class with Property Getter and Setter
Imperative
Create and Use a Mutable Value
Loop from 0 up to 100
Loop from 100 down to 0
Functional
Declare and Use a Curried Function
Generate a List of Numbers Using a Formula
Combine the Elements of Two Lists into a List of Pairs
.NET
Convert an int into an Enum Value
Convert an Enum into an int Value
Object Oriented
Simple Class with Default Constructor
#light
type SimpleClass() =
member sc.SimpleMemberFunction() = 0
Simple Class with Constructor Parameters
#light
type SimpleClass(someValue:int) =
member sc.SimpleMemberFunction() = someValue
Class with Multiple Constructors
#light
type MyPoint(x,y) =
new () = MyPoint(0,0)
Simple Class Inheritance
#light
type Shape() = class
end
type Rectangle(width:float,height:float) =
inherit Shape()
member rect.Width = width
member rect.Height = height
type Square(size) =
inherit Rectangle(size,size)
type Circle(diameter:float) =
inherit Shape()
member circle.Diameter = diameter
Declare and Use an Interface
#light
type IOneAdder =
abstract AddOne : int -> int
type OneAdder() =
interface IOneAdder with
member oa.AddOne(i) = i+1
Inherit an Interface from another Interface
#light
type IInterface1 =
abstract MyMethod1 : unit -> unit
type IInterface2 =
inherit IInterface1
abstract MyMethod2 : unit -> unit
Anonymously Implement an Interface
#light
type IOneAdder =
abstract AddOne : int -> int
let oneAdder =
{ new IOneAdder with
member oa.AddOne(i)=i+1 }
printf "1+1=%d" (oneAdder.AddOne(1))
Interface with no Members
#light
type IEmpty = interface
end
Class with Property Getter and Setter
#light
type MyClass() =
let mutable value = 5
member mc.MyProperty
with get() = value
and set(x) = value<-x
Functional
Declare and Use a Curried Function
#light
let CurriedFunction x y =
printf "x=%d y=%d" x y
// Create a new function that always has the value 3 for parameter 'x'
let PartiallyApplied y =
CurriedFunction 3 y
// Call partially applied function
PartiallyApplied 9
PartiallyApplied 52
Sum the Elements in a List
#light
let numbers = [1;2;3;5;7;11]
let sum = numbers |> List.fold_left (+) 0
Generate a List of Numbers Using a Formula
#light
let evens = [for i in 0..100 -> i*2 ]
let odds = [for i in 0..100 -> i*2 + 1]
evens;;
val it : int list
= [0; 2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; 38;
40; 42; 44; 46; 48; 50; 52; 54; 56; 58; 60; 62; 64; 66; 68; 70; 72; 74; 76;
78; 80; 82; 84; 86; 88; 90; 92; 94; 96; 98; 100; 102; 104; 106; 108; 110;
112; 114; 116; 118; 120; 122; 124; 126; 128; 130; 132; 134; 136; 138; 140;
142; 144; 146; 148; 150; 152; 154; 156; 158; 160; 162; 164; 166; 168; 170;
172; 174; 176; 178; 180; 182; 184; 186; 188; 190; 192; 194; 196; 198; ...]
Simple Discriminated Union
#light
/// A small logic programming language.
type Expr =
/// The literal truth.
| True
/// Negates the truth value of an expression.
| Not of Expr
/// Logical 'and' of two expressions.
| And of Expr * Expr
/// Logical 'or' of two expressions.
| Or of Expr * Expr
.NET
Declare an Enum
#light
type Color =
Black = 0
| White = 1
| Red = 2
| Blue = 3
| Yellow = 4
Convert an int into an Enum Value
#light
type Color =
Black = 0
| White = 1
| Red = 2
| Blue = 3
| Yellow = 4
let red = enum 2 : Color
Convert an Enum into an int Value
#light
type Color =
Black = 0
| White = 1
| Red = 2
| Blue = 3
| Yellow = 4
let two = (int) Color.Red