Tech Tock

Time is of the essence.

Functional Homework

MejerTShirtAs I mentioned last month, I want to learn a functional programming language.  Lucky for me, Dr. Erik Meijer (and his electric t-shirt) gave a course on Haskell in October and its posted on Channel 9.

I watched the first lecture over a week ago but haven’t had time to do the homework till now.

Here it is in C#.  Its quicksort written as he described it in Haskell.  It doesn’t feel that different from the programming I usually do, but it is recursive (which I generally avoid), so maybe that’s something.  At least I did my homework, now I can go on to the second lecture :).

As long as I was making a program, I figured I’d expand my WPF horizons and used an attached behavior for the button.  Of course its MVVM which is just a habit now.

Attached Behaviors

It wasn’t obvious how to link the static attached behavior to the instance properly.

A guy on StackOverflow recommended getting the DataContext from the sender in the event:

private static void ButtonOnClick(object sender, RoutedEventArgs args)

var fxElem = sender as FrameworkElement;

var vm = (VM) ((FrameworkElement)args.OriginalSource).DataContext;

Most other explanations of attached behaviors are limited to working with the control itself and not the ViewModel:

You can download the code here.


Here’s the homework code:

public  IEnumerable<IComparable>
    Sort(IEnumerable<IComparable> listToSort)
    if (listToSort == null)
        throw new InvalidOperationException
            ("Cannot sort a null list.");
    if (listToSort.Count() == 0)
        return listToSort;
    var result = new List<IComparable>();
    var start = listToSort.First();
    //smaller on left
        .Where(e => e.CompareTo(start) <= 0)));
    //val in middle
    //larger on right
        .Where(e => e.CompareTo(start) > 0)));
    return result;

Here’s some highlights of the attached behavior class:

public static class SortStarter


// // RegisterAttached for attached behaviors

public static readonly DependencyProperty SortBehaviorProperty =

DependencyProperty.RegisterAttached(“SortBehavior”, typeof(bool), typeof(SortStarter), new UIPropertyMetadata(false, SortBehaviorChanged));

// Get/Set Methods for the attached behaviors:

public static bool GetSortBehavior(DependencyObject obj){

return (bool)obj.GetValue(SortBehaviorProperty);}

public static void SetSortBehavior(DependencyObject obj, bool value){

obj.SetValue(SortBehaviorProperty, value);}

// Method to hook the event

private static void SortBehaviorChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args){

var button = dpo as ButtonBase;

if (button != null){

if ((bool) args.NewValue)

button.Click += ButtonOnClick;


button.Click -= ButtonOnClick;


// Button Click Handler:

private static void ButtonOnClick(object sender, RoutedEventArgs args){

// Don’t process calls from up the visual tree

if (!Object.ReferenceEquals(sender, args.OriginalSource))  return;

//get VM from DC

var fxElem = sender as FrameworkElement;

var vm = ((FrameworkElement) args.OriginalSource).DataContext as VM;

// process with VM and new Sorter class


Hooking up the button in XAML is simple:

<Window x:Class=”WpfApplication1.Window1″




Title=”Window1″ Height=”129″ Width=”300″>

<Button local:SortStarter.SortBehavior=”True” >Sort</Button>


December 26, 2009 - Posted by | Uncategorized | , , ,

1 Comment »

  1. […] I’m glad I took the class, but I’ll be happy to get one night a week back.  Hopefully I can find time to finish Haskell. […]

    Pingback by Weeks @Lab « Tech Tock | March 15, 2010 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: