Tuesday, March 31, 2009

Bluetooth headset winner – the Jawbone 2

I’m using a Jawbone 2 now.  I’ve had it for a couple weeks, and it’s working great.  Good sound quality on both sides, works fine outside and in the car.  (If you’re buying from Amazon, don’t buy the bulk package version.  According to the reviews, it doesn’t come with all the right extra bits.)  And make sure you’re getting a version 2, not the original Jawbone – the original is terrible.

The previous headset I tried was a Plantronics Discovery 925.  Indoors, it was fine.  Outdoors, it was useless.  A slight breeze and the person I was calling said it sounded like I was in a hurricane.

Friday, March 27, 2009

Hello, World in F# + WPF

Here’s the small version of F# + WPF Hello, world.  Note that there’s zero Xaml involved.
#light

open System
open System.Windows
open System.Windows.Controls

type MainWindow(app: Application) as self =
  inherit Window()
  do
    let button = Button(Content = "Bye")
    button.Click.Add(fun f -> app.Shutdown(0))
    self.Content <- button

type App() =
  inherit Application()
  
  static member Go() =
    let app = new App()
    app.Run(new MainWindow(app, Title = "Hello world")) 
    |> ignore

[<STAThread>] App.Go()

The references are very similar to the C# code:
image

Thursday, March 26, 2009

Starting out with WPF in F# – but first, C#

I've started to use F# with WPF, and thought that the best way of going about it would be to start with just bare code for the most stripped-down C# app that would do anything.

Most of the WPF examples out there start with too much Xaml - seems like it's just going to get in the way for now.

Norbert Eder's blog had the meat of this - I just stripped it down even more.

using System;
using System.Windows;
using System.Windows.Controls;

public class App : Application
{
[STAThread]
static void Main(string[] args)
{
Application app = new Application();
app.Run(new MainWindow());
}
}

public class MainWindow : Window
{
public MainWindow()
{
Button button1 = new Button();
button1.Content = "Bye";
button1.Click += new RoutedEventHandler(button1_Click);
this.Content = button1;
this.Title = "Hello world";
}

void button1_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown(0);
}
}

The project only needs a couple references – PresentationCore, PresentationFramework, System, and WindowsBase:

image  

 

Tuesday, March 10, 2009

F# useful links

The spec I use this all the time, for obvious reasons.

Expert F# (I use it on Safari - this is the incomplete/sample Google Books entry)

The basic syntax of F# - classes, interfaces, and members Useful disection of an F# type declaration

F Sharp Programming - Wikibooks, collection of open-content textbooks Occasionally useful, but somewhat out of date.  Better to turn to Expert F#.

hubFS forums Lots of useful information in the hubFS forums.  (Think of this as the F# mailing lists; for whatever reason, the F# community seems to prefer forums to mailing lists.)

Sep 2008 release known issues

Tuesday, March 3, 2009

F#: The type 'MatchCollection' is not compatible with the type 'seq<'a>'.

I was trying to use regular expressions with sequences, and ran across this problem.
(new Regex ".*").Matches("foo") |> Seq.iter (fun s -> printfn "s is %A" s);;
-----------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stdin(4,36): error FS0001: The type 'MatchCollection' is not compatible with the type 'seq<'a>'.
> (new Regex "f.*").Matches("foo") |> Seq.cast |> Seq.iter (fun s -> printfn "s is %A" s);;
s is foo
val it : unit = ()
>
Turns out MatchCollection implements System.Collections.IEnumerable, not System.Collections.Generic.IEnumerable<T>. You need to cast it with Seq.cast to use it:
(new Regex "f.*").Matches("foo") |> Seq.cast |> Seq.iter (fun s -> printfn "s is %A" s);;
I got the solution from hubfs - it's a good place for F# information.

F# error talking complaining about FSharp.PowerPack.dll

If your F# code compiles, but you see errors like this from fsi:

C:\Users\James\Documents\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\FSharpDictionary.fs(108,13): error FS0039: The value, constructor, namespace or type 'contains' is not defined. A construct with this name was found in FSharp.PowerPack.dll, which contains some modules and types that were implicitly referenced in some previous versions of F#. You may need to add an explicit reference to this DLL in order to compile this code.
>

Make sure you add PowerPack to your session:

> #r "FSharp.PowerPack.dll" ;;

My problem was that I had added FSharp.PowerPack to the Visual Studio project, but I had forgotten that fsi doesn't pick that up.

And with the new 2010 release, you're going to need to add the location of PowerPack to your search path in fsi.exe:

  > #r "FSharp.PowerPack.dll" ;;
    > #I "C:\Users\James\Documents\Visual Studio 10\Projects\fspowerpack"

Watch out for quoting backspaces in strings.