Fruit basket program in F#

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 03 Mar 2010 20:17:04 -0500

From the archive at
http://EmpowermentZone.com/fs_fruit.zip

This fruit basket program is written in F# (pronounced F Sharp). After several years as a Microsoft research project, F# is becoming a first class language for the .NET platform. It primarily implements a functional style of programming, which tends to be well-suited for scientific and parallel computing. Unlike other functional languages, F# also supports imperative and object oriented styles.

F# development is supported in Visual Studio, though not currently in an Express Edition. A command-line compiler is also available, as well as an interactive environment for testing code, similar to that of Python or Ruby.

By default, indentation is required in F#, similar to Python. This is called the "light" syntax. An alternate syntax, similar to the ML language, is also possible, where keywords or punctuation marks are used to denote code blocks instead of indentation. I found that syntax hard to do with the current compiler, however, because such error messages are less helpful -- probably because the compiler has been tested far less in this way.

Various F# resources are available at
http://FSharp.net

I have also collected text tutorials at
http://EmpowermentZone.com/fnetdoc.zip

Once the F# development kit is installed, the source code fs_fruit.fs, also pasted below, may be compiled to fs_fruit.exe via the batch file compile.bat. Since F# related assemblies are not installed with versions of the .NET Framework below 4.0, they either have to be distributed with an F# program as separate files, or built into the same executable with the --standalone compiler option. I used this option, resulting in an executable size of nearly a megabyte. By comparison, the version with dependencies is about 10K.

Jamal


// Uncomment the following line for syntax that does not require indentation
//# indent "off"

(*
content of fs_fruit.fs
Fruit Basket program in F#
Public domain by Jamal Mazrui
*)

// Import namespaces
open System
open System.Windows.Forms

// Create controls
let tlp = new TableLayoutPanel(ColumnCount = 3, RowCount = 2)
let lblFruit = new Label(Text = "&Fruit:", Parent = tlp)
let txtFruit = new TextBox(Parent = tlp)
let btnAdd = new Button(Text = "&Add", Parent = tlp)
let lblBasket = new Label(Text = "&Basket:", Parent = tlp)
let lstBasket = new ListBox(Parent = tlp)
let btnDelete = new Button(Text = "&Delete", Parent = tlp)

// Define Add event handler
btnAdd.Click.Add(fun o ->
    let sFruit = txtFruit.Text
    if sFruit = "" then
        ignore(MessageBox.Show("No fruit to add!"), "Alert")
    else
        txtFruit.Clear()
        let iIndex = lstBasket.Items.Add(sFruit)
        lstBasket.SelectedIndex <- iIndex)

// Define Delete event handler
btnDelete.Click.Add(fun o ->
    let iIndex = lstBasket.SelectedIndex
    if iIndex = -1 then
        ignore(MessageBox.Show("No fruit to delete."), "Alert")
    else
        lstBasket.Items.RemoveAt(iIndex)
if iIndex = lstBasket.Items.Count then lstBasket.SelectedIndex <- iIndex - 1
        else lstBasket.SelectedIndex <- iIndex)

// Finalize dialog
let dlg = new Form(Text = "Fruit Basket", AcceptButton = btnAdd, StartPosition = FormStartPosition.CenterScreen, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink)
dlg.Controls.Add(tlp)
ignore(dlg.ShowDialog())

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts:

  • » Fruit basket program in F# - Jamal Mazrui