Playing with F#

This little pretty window:

is a result of this tiny bit of F# code:

#light

open System
open Gtk

Application.Init()

let window = new Gtk.Window(“F# Gtk”)
let vBox = new Gtk.VBox()
let closeButton = new Gtk.Button()
let label = new Gtk.Label(“Hello World!”)

window.WindowPosition < – Gtk.WindowPosition.Center
window.SetDefaultSize(160, 40)
window.Destroyed.Add(fun _ -> Application.Quit() )

closeButton.Label < – “Close”
closeButton.Clicked.Add(fun _ -> Application.Quit() )

vBox.BorderWidth < – (uint32) 6
vBox.PackStart(label, false, false, (uint32) 6)
vBox.PackStart(closeButton, false, false, (uint32) 6)

window.Add(vBox)
window.ShowAll()

//brown paperbag update: I’ve missed this line during Copy/Paste
Application.Run()

Next step is piece of code that actually does something semi-useful. I’m thinking about implementing a convex hull algorithm which F# seems pretty well suited for.

Tags: , ,

5 Responses to “Playing with F#”

  1. Alexy Says:

    Piotr — ran that in Mono, the window does nothing, Close button doesn’t work, nothing reacts. Should there be a wait loop somewhere?

  2. piotr Says:

    @Alexy Runs fine on my machine, but I compile it in Windows (and .Net) and run it on Mono 2.0.
    You can try my compiled exe from here:
    http://dl.getdropbox.com/u/118505/FsGtk.exe

  3. Alexy Says:

    Ah — I do it from fsi, the toplevel. Once I #load it, is shows something like,

    namespace FSI_0013
    val window : Window
    val vBox : VBox
    val closeButton : Button
    val label : Label

    >

    – then the window is abandoned and doesn’t respond. I wonder if I should add something to run from F# Interactive…

  4. Alexy Says:

    Well, I tried 3 different Gtk frameworks for Mac and am in the process of compiling gtk-sharp-2.12 on cygwin in VMWare Fusion, and adding the last line fixed everything! :) Thanks, man! :)

  5. piotr Says:

    Not a problem. I should have picked it up earlier. Sorry… :-)

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes