Home

F# Topics on Functions: Applying Values to Functions

 

Fundamentals

 

Introduction

Applying a value to a function consists of passing the value to it. Here is an example:

let convertToPercent x = x * 100.00

let result = convertToPercent 0.875

F# has a technique to formally pass a value to a function. This is referred to as applying a value to a function. This is done using the <| operator. The formula to follow is:

Function-name <| Value

You start with the function name, followewd by the < operator and followed by the value. Here is an example:

open System
open System.Windows.Forms

let convertToPercent x = x * 100.00

let businessMathematics = new Form()
businessMathematics.Text   <- "Business Mathematics"
businessMathematics.Width  <- 210
businessMathematics.Height <- 165

let lblValue = new Label()
lblValue.Left  <- 18
lblValue.Top   <- 18
lblValue.Width <- 65
lblValue.Text  <- "Value:"
businessMathematics.Controls.Add lblValue

let txtValue = new TextBox()
txtValue.Left  <- 90
txtValue.Top   <- 16
txtValue.Width <- 75
txtValue.Text  <- "0.00"
businessMathematics.Controls.Add txtValue

let btnConvert = new Button()
btnConvert.Left  <- 90
btnConvert.Top   <- 44
btnConvert.Text  <- "Convert"

let lblConverted = new Label()
lblConverted.Left  <- 18
lblConverted.Top   <- 78
lblConverted.Width <- 70
lblConverted.Text  <- "Converted:"
businessMathematics.Controls.Add lblConverted

let txtConverted = new TextBox()
txtConverted.Left  <- 90
txtConverted.Top   <- 74
txtConverted.Width <- 45
txtConverted.Text  <- "0.00"
businessMathematics.Controls.Add txtConverted

let lblPercent = new Label()
lblPercent.Left  <- 136
lblPercent.Top   <- 77
lblPercent.Width <- 20
lblPercent.Text  <- "%"
businessMathematics.Controls.Add lblPercent

let btnConvertClick e =
    let value = float txtValue.Text

    let result = convertToPercent <| value
    let strResult = sprintf "%0.02f" result

    txtConverted.Text <- strResult

btnConvert.Click.Add btnConvertClick
businessMathematics.Controls.Add btnConvert

let btnClose = new Button()
btnClose.Left  <- 90
btnClose.Top   <- 104
btnClose.Width <-  75
btnClose.Text  <- "Close"
let btnCloseClick e = businessMathematics.Close()
btnClose.Click.Add btnCloseClick
businessMathematics.Controls.Add btnClose

do Application.Run businessMathematics

Here is an example of executing the program:

Applying a Value to a Function

Instead of defining a formal function, you can use a lambda expression in place of the function. Here is an example:

let btnConvertClick e =
    let value = float txtValue.Text

    let result = (fun x -> x * 100.00) <| value
    let strResult = sprintf "%0.02f" result

    txtConverted.Text <- strResult

Applying a Value to the Left Side of a Function

The previous formula had the function on the left of the operator while the value was on the right. An alternative is to first specify the value before the function. This is referred to as applying the value to the left side. This time, you us the |> operator. The formula to follow is:

Value |> Function-name

Notice that in both cases, the pipe (|) specifies the starting point and the arrow indicates the direction of the application. Here is an example:

let btnConvertClick e =
    let value = float txtValue.Text

    let result = value |> convertToPercent
    let strResult = sprintf "%0.02f" result

    txtConverted.Text <- strResult
     
 

Applying Many Values to a Function

If a function takes more than one argument, you can apply them to the right side of the function, each value preceded by its own <| operator. Here is an example:

let calculateMarkedPrice price discount = price * (100.00 - discount) / 100.00

let netPrice = calculateMarkedPrice <| 145.50 <| 25.00

printfn "Net Price: %0.02f" netPrice

In the same way, you can apply as many values as necessary to a function.

Applying Values on Both Sides of a Function

If a function takes two arguments, you can apply one to the left and the other to the right side of the function. Here is an example:

let calculateMarkedPrice price discount = price * (100.00 - discount) / 100.00

let netPrice = 145.50 |> calculateMarkedPrice <| 25.00

printfn "Net Price: %0.02f" netPrice

If a function takes more than two varguments, you can apply one to the left and the others to the right side of the function. Here is an example:

let calculateNetDecimalEquivalent a b c =
    let complementA = (100.00 - a) / 100.00
    let complementB = (100.00 - b) / 100.00
    let complementC = (100.00 - c) / 100.00
    complementA * complementB * complementC

let calculateNetPrice price netDecimal = price * netDecimal

let nd = 20.00 |> calculateNetDecimalEquivalent <| 10.00 <| 5.00
let netPrice = 2500.00 |> calculateNetPrice <| nd

printfn "Net Decimal: %0.03f" nd
printfn "Net Price:   %0.02f" netPrice

Composing Lambda Expressions

In the previous examples, we first defined the function(s) to compose. As an alternative, you can create the function(s) directly in the placeholder. This is done by creating (a) lambda expression(s). Here are examples:

let hourlySalary = 25.85          

let yearEvaluation = (fun salary -> salary * 8.00) >> (fun value -> value * 5.00) >> (fun value -> value * 2.00) >> (fun value -> value * 2.00) >> (fun value -> value * 12.00)

let yearlySalary = yearEvaluation hourlySalary

sprintf "Yearly Salary: %0.0f" yearlySalary

Remember that, to make your code easy to read, you can write each function preceded by its << or >> on its own line with proper indentation. Here are examples:

let hourlySalary = 25.85          

let yearEvaluation =
       (fun salary -> salary * 8.00)
    >> (fun value  -> value  * 5.00)
    >> (fun value  -> value  * 2.00)
    >> (fun value  -> value  * 2.00)
    >> (fun value  -> value  * 12.00)
let yearlySalary = yearEvaluation hourlySalary
   
   
 

Previous Copyright © 2009-2015, FunctionX Next