This project is read-only.
Define operator - inline function

Category:
Defining Operator
Description: Define operator using inline function
Code:
let DefineOperator() =
let inline (++) x y = x+y*2
let a = 9
let r = (a++4)
printfn "%d" r
Assert.AreEqual(r, 17)

Execution Result:
17
both elements are equal

Define prefix operator

Category:
Defining Operator
Description: Define prefix operator
Code:
let DefineOperator2() =
let inline (~+.) x = x+1
let a = 9
let r = (+.a)
printfn "%d" r
Assert.AreEqual(r, 10)

Execution Result:
10
both elements are equal

Define operator

Category:
Defining Operator
Description: Define Operator - file path operator
Code:
let DefineOperator4() = 
let (+/) path1 path2 = System.IO.Path.Combine(path1, path2)
let (+/) path1 path2 = System.IO.Path.Combine(path1, path2)
let root = @"C:\"
let filePath = root +/ "Subdirectory" +/ "Test.fsx"
printfn "the file path is %s" filePath

Execution Result:
the file path is C:\Subdirectory\Test.fsx

Define operator

Category:
Defining Operator
Description: Define operators in a class.
Code:
let rec hcf a b =
if a = 0u then b
elif a then hcf a (b - a)
else hcf (a - b) b
type Fraction =
{
// n: Numerator of fraction.
n : uint32
// d: Denominator of fraction.
d : uint32
}

// Produce a string representation. If the
// denominator is "1", do not display it.
override this.ToString() =
if (this.d = 1u)
then this.n.ToString()
else this.n.ToString() + "/" + this.d.ToString()

// Add two fractions.
static member (+) (f1 : Fraction, f2 : Fraction) =
let nTemp = f1.n * f2.d + f2.n * f1.d
let dTemp = f1.d * f2.d
let hcfTemp = hcf nTemp dTemp
{ n = nTemp / hcfTemp; d = dTemp / hcfTemp }

// Adds a fraction and a positive integer.
static member (+) (f1: Fraction, i : uint32) =
let nTemp = f1.n + i * f1.d
let dTemp = f1.d
let hcfTemp = hcf nTemp dTemp
{ n = nTemp / hcfTemp; d = dTemp / hcfTemp }

// Adds a positive integer and a fraction.
static member (+) (i : uint32, f2: Fraction) =
let nTemp = f2.n + i * f2.d
let dTemp = f2.d
let hcfTemp = hcf nTemp dTemp
{ n = nTemp / hcfTemp; d = dTemp / hcfTemp }

let DefinteOperator3() =
let fraction1 = { n = 3u; d = 4u }
let fraction2 = { n = 1u; d = 2u }
let result1 = fraction1 + fraction2
let result2 = fraction1 + 1u
printfn "result1 = %s" (result1.ToString())
printfn "result2 = %s" (result2.ToString())

Execution Result:
result1 = 5/4
result2 = 7/4

Define function

Category:
Defining Operator
Description: define a function and composite functions and pipeline
Code:
let DefineFunction1() =
let f1 a = a + 1
let f2 a = a * 10
let ff = f1 >> f2
let r = ff 3
let r1 = 3 |> f1 |> f2
printfn "result = %d and result1 = %d" r r1
Assert.AreEqual(r, r1)

Execution Result:
result = 40 and result1 = 40
both elements are equal

Use function keyword

Category:
Defining Operator
Description: Use function keyword
Code:
type Patterns = 
| Pattern0
| Pattern1

let DefineFunction2() =
let p = Patterns.Pattern0
let matchResult = function
| Pattern0 -> "pattern0"
| Pattern1 -> "pattern1"
printfn "%A" (matchResult p)

Execution Result:
"pattern0"

Last edited Sep 13, 2011 at 12:15 AM by ttliu2000, version 1

Comments

No comments yet.