Home

F# Collections: Arrays and Lists

   

Description

If you have an existing list, you can easily convert it into an array. To support this operation, the List module provides a function named toArray. Its signature is:

This function takes one argument as a list. The function returns an array made of the items of the list. Here is an example of calling this function:

open System
open System.Drawing
open System.Windows.Forms 

let mutable temporary = []

type MachineUsageDepreciation(cost, salvage, life, usage : int array) =
    let mutable hrs : int array = usage
    let mutable counter = 0

    member this.HourlyDepreciation with get() = (cost - salvage) / life
    member this.Operations with get() = hrs and set(hours) = hrs <- hours
    member this.YearlyDepreciation with get() = [| for nbr in hrs do yield float nbr * this.HourlyDepreciation |]
    member this.DepreciationSchedule with get() =
                                        [| for n in hrs do yield (float n, float n * this.HourlyDepreciation) |]

    new(cost, salvage, life) = MachineUsageDepreciation(cost, salvage, life, [||])

let unitsOfProduction : Form = new Form(MaximizeBox = false, Text = "Depreciation: Units of Production", ClientSize = new System.Drawing.Size(325, 370), StartPosition = FormStartPosition.CenterScreen)

unitsOfProduction.Controls.Add(new Label(Location = new Point(21, 25), AutoSize = true, Text = "Machine Cost:"))
let txtMachineCost : TextBox = new TextBox(Location = new Point(130, 22), Text = "0.00", TextAlign = HorizontalAlignment.Right, Width = 75)
unitsOfProduction.Controls.Add txtMachineCost

unitsOfProduction.Controls.Add(new Label(Location = new Point(21, 54), AutoSize = true, Text = "Salvage Value:"))
let txtSalvageValue : TextBox = new TextBox(Location = new Point(130, 51), Text = "0.00", TextAlign = HorizontalAlignment.Right, Width = 75)
unitsOfProduction.Controls.Add txtSalvageValue

unitsOfProduction.Controls.Add(new Label(Location = new Point(21, 83), AutoSize = true, Text = "Estimated Life:"))
let txtEstimatedLife : TextBox = new TextBox(Location = new Point(130, 80), Text = "0.00", TextAlign = HorizontalAlignment.Right, Width = 52)
unitsOfProduction.Controls.Add txtEstimatedLife
unitsOfProduction.Controls.Add(new Label(Location = new Point(187, 83), AutoSize = true, Text = "Hours"))

let lblLine = new Label(Location = new Point(21, 105), AutoSize = true)
lblLine.Text <- "Yearly Operations ________________________________"
unitsOfProduction.Controls.Add lblLine

unitsOfProduction.Controls.Add(new Label(Location = new Point(21, 135), AutoSize = true, Text = "Year:"))
let txtYearlyHours : TextBox = new TextBox(Location = new Point(130, 132), Text = "0.00", TextAlign = HorizontalAlignment.Right, Width = 52)
unitsOfProduction.Controls.Add txtYearlyHours
unitsOfProduction.Controls.Add(new Label(Location = new Point(187, 135), AutoSize = true, Text = "Hours/Year"))

unitsOfProduction.Controls.Add(new Label(Location = new Point(21, 83), AutoSize = true, Text = "Estimated Life:"))

unitsOfProduction.Controls.Add(new Label(Location = new Point(21, 209), AutoSize = true, Text = "Hourly Depreciation:"))
let txtHourlyDepreciation : TextBox = new TextBox(Location = new Point(130, 206), Text = "0.00", TextAlign = HorizontalAlignment.Right, Width = 52)
unitsOfProduction.Controls.Add txtHourlyDepreciation
unitsOfProduction.Controls.Add(new Label(Location = new Point(187, 209), AutoSize = true, Text = "/Hour"))

unitsOfProduction.Controls.Add(new Label(Location = new Point(55, 240), AutoSize = true, Text = "Depreciation Schedule:"))

let lvwDepreciationSchedule : ListView = new ListView(Location = new Point(50, 258), Size = new System.Drawing.Size(225, 95), GridLines = true, View = View.Details, FullRowSelect = true)
lvwDepreciationSchedule.Columns.Add("Year", 40) |> ignore
lvwDepreciationSchedule.Columns.Add("Usage", 95, HorizontalAlignment.Center) |> ignore
lvwDepreciationSchedule.Columns.Add("Depreciation", 85, HorizontalAlignment.Right) |> ignore

unitsOfProduction.Controls.Add lvwDepreciationSchedule

let btnAdd : Button = new Button(Location = new Point(255, 130), Width = 50, Text = "Add")
let btnAddClick e =
    temporary <- temporary @ [ int txtYearlyHours.Text ]

    txtYearlyHours.Text <- "0.00"
    txtYearlyHours.Focus() |> ignore

btnAdd.Click.Add btnAddClick
unitsOfProduction.Controls.Add btnAdd

let btnCalculate : Button = new Button(Location = new Point(24, 163), Size = new System.Drawing.Size(280, 31), Text = "Calculate")
let btnCalculateClick e =
    let machineCost   = float txtMachineCost.Text
    let salvageValue  = float txtSalvageValue.Text
    let estimatedLife = float txtEstimatedLife.Text

    let timesUsed = List.toArray temporary

    let depreciation = new MachineUsageDepreciation(machineCost, salvageValue, estimatedLife, timesUsed)
    txtHourlyDepreciation.Text <- sprintf "%0.03f" depreciation.HourlyDepreciation

    lvwDepreciationSchedule.Items.Clear()

    let mutable year = 1
    for usage in depreciation.DepreciationSchedule do
        let (hour, depreciation) = usage

        let lviDepreciation = new ListViewItem(sprintf "%i" year)
        lviDepreciation.SubItems.Add(sprintf "%0.0f Hours/Year" hour) |> ignore
        lviDepreciation.SubItems.Add(sprintf "$%0.0f" depreciation) |> ignore
        lvwDepreciationSchedule.Items.Add lviDepreciation |> ignore
        year <- year + 1
btnCalculate.Click.Add btnCalculateClick
unitsOfProduction.Controls.Add btnCalculate

[<EntryPoint>]
let main argv =
    Application.Run unitsOfProduction
    0

On the other hand, if you have an array of values and you need to use it as a list, to let you convert it into a list, the Array module is equied with a function named toList. Its signature is:

Array.toList : 'T [] -> 'T list

This function returns a list from an array that is passed to it.

     
     
 

Home Copyright © 2012-2015, FunctionX Home