Geeks With Blogs
Mark Pearl June 2010 Entries
C# Lambda & Predicate Basics
Okay… don’t judge me… I have been coding in C# for a few years now and up till the point that I got into F# I never used lambda’s. This last week I realized just how much I am using the lambda expression in F# and thought it was about time I exposed myself to it in C#… So first of all, the lambda expression in C# is symbolized by the =>. From what I understand it is really syntactical sugar over the C# language to anonymous delegates, which have been in the language for sometime. I am not going ......

Posted On Tuesday, June 29, 2010 9:05 PM

F# – Put that in your pipe and smoke it |>
So I must admit, the first time I saw it I scratched my head and thought to myself, why will I ever use that? Several months since then and now I love the Forward Pipe (FP) in F#. Why? For me, the the FP makes me think of a problem in a different way. In C# when I attempt to solve I problem I typically break it down in steps, once I have reached the end result I have my smallest step, and then I work my methods backwards so that I can get back to the original input… in a very real way it makes me ......

Posted On Monday, June 28, 2010 6:34 PM

F# Euler Problem 6
Today I had a quick bash at problem 6. To me this has been the easiest of the problems so far. It is the first time though that I got to use the Seq.Fold function which was good to get exposure to. I am not to happy with my code though, I can’t explain it, but it just seems a bit messy… Anyhow, I look forward to feedback from everyone in the F# community on their approaches. Problem The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the ......

Posted On Saturday, June 26, 2010 7:25 PM

F# FizzBuzz
As per the post I decided to do a F# attempt at FizzBuzz. It took a few seconds… Using FizzBuzz to Find Developers who Grok Coding.. let FizzBuzz = seq {1..100} |> Seq.iter (fun x -> match x with | x when x % 5 = 0 && x % 3 = 0 -> printfn "FizzBuzz" | x when x % 3 = 0 -> printfn "Fizz" | x when x % 5 = 0 -> printfn "Buzz" | _ -> printfn "%d" x) ......

Posted On Friday, June 25, 2010 5:48 PM

Tech Support Secret Handshake - Fizzbuzz
I hope I spelt “Fizzbuzz” correctly, but I had a real laugh listening to Hansel Minutes Show 218 when Scott suggested that we have a secret password to give to Tech Support so that they know we aren’t idiot’s. I think it is a great idea… So a typical call would go as follows… You) Hi, I need help with my internet connection Tech Support) Yes Sir, click the big button on the bottom left corner of your screen called the “start button” You) Fizzbuzz Tech Support) Okay, so you have obviously checked ......

Posted On Friday, June 25, 2010 3:26 PM

F# Euler Problem 5
So today I tackled a Euler problem that I had originally looked into several months back when I initially started exploring F# Problem 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? Solution I decided on the brute force approach again. I believe that I could have worked with prime factors etc to have reached the solution a lot quicker – but ......

Posted On Friday, June 25, 2010 3:14 PM

F# - Euler Problem 4
The last few days I began to play around with problem 4 of Euler. I really enjoyed this problem since it dealt with a few functions in F# that I haven’t dealt with in the past. Problem A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. Find the largest palindrome made from the product of two 3-digit numbers. Solution So I divided the problem up into a few functions… reverseString simply reverses a string. isPalindrome ......

Posted On Thursday, June 24, 2010 7:02 PM

F# Seq.Unfold
So I have been playing around with more Euler problems and I have found it a great way to learn some of the basic functions of F# and Seq specifically. The more I play the more I am really loving F# and how succinct the language is. Today I thought I would put up a brief post on the Seq.unfold function. So, I see this function being used everywhere… my understanding of it is that it is a way that one can build a sequence based on a series of commands. I will use a small code snippet to show an example ......

Posted On Wednesday, June 23, 2010 7:28 PM

Another good Inversion of Control reference

 

Another great IOC article I saw that I thought would be worth keeping on record

http://devlicio.us/blogs/krzysztof_kozmic/archive/2010/06/20/how-i-use-inversion-of-control-containers.aspx

Posted On Monday, June 21, 2010 6:18 PM

F# Project Euler Problem 3
So this weekend I made an attempt at problem 3 of Project Euler. Problem 3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? My Solution // // Checks whether a number is prime or not using the Trial Division approach // To improve performance I have only pass through odd numbers greater than 2 and do not check for this case // let isPrime (number : bigint) = match number with | _ -> seq { bigint(2) .. bigint(1) .. bigint (Math.Sqrt (float ......

Posted On Monday, June 21, 2010 8:07 AM

F# Project Euler Problem 2
Today I thought I would give a bash at problem 2 of Euler. Problem 2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million. My Solution (Brute Force) So first of all I needed a Fibonacci generator. I resorted to one that recursively defines the Fibonacci number based on the index with ......

Posted On Saturday, June 19, 2010 1:49 PM

F# Project Euler Problem 1
Every now and then I give project Euler a quick browse. Since I have been playing with F# I have found it a great way to learn the basics of the language. Today I thought I would give problem 1 an attempt… Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. My F# Solution I broke this problem into two functions… 1) be able to generate a collection of numbers ......

Posted On Thursday, June 17, 2010 9:56 PM

Yippy – the F# MVVM Pattern
I did a recent post on implementing WPF with F#. Today I would like to expand on this posting to give a simple implementation of the MVVM pattern in F#. A good read about this topic can also be found on Dean Chalk’s blog although my example of the pattern is possibly simpler. With the MVVM pattern one typically has 3 segments, the view, viewmodel and model. With the beauty of WPF binding one is able to link the state based viewmodel to the view. In my implementation I have kept the same principles. ......

Posted On Thursday, June 17, 2010 2:04 AM

5 ways to stop code thrashing…
A few days ago I was programming on a personal project and hit a roadblock. I was applying the MVVM pattern and for some reason my view model was not updating the view when the state changed??? I had applied this pattern many times before and had never had this problem. It just didn’t make sense. So what did I do… I did what anyone would have done in my situation and looked to pass the blame to someone or something else. I tried to blame one of the inherited base classes, but it looked fine, then ......

Posted On Wednesday, June 16, 2010 3:40 PM

F# WPF Form – the basics
I was listening to Dot Net Rocks show #560 about F# and during the podcast Richard Campbell brought up a good point with regards to F# and a GUI. In essence what I understood his point to be was that until one could write an end to end application in F#, it would be a hard sell to developers to take it on. In part I agree with him, while I am beginning to really enjoy learning F#, I can’t but help feel that I would be a lot further into the language if I could do my Windows Forms like I do in C# ......

Posted On Thursday, June 10, 2010 9:02 PM

Simple Mouse Move Event in F# with Winforms
This evening I had the pleasure of reading one of ThomasP’s blog posts on first class events. It was an excellent read, and I thought I would make a brief derivative of his post to explore some of the basics. In Thomas’s post he has a form with an ellipse on it that when he clicks on the ellipse it pops up a message box with the button clicked… awesome. Something that got me on the post though was the code similar to the one below… // React to Mouse Move events on the form let evtMessages = frm.MouseMove ......

Posted On Wednesday, June 9, 2010 9:45 PM

F# – Immutable List vs a Mutable Collection in Arrays
Another day gone by looking into F#. Today I thought I would ramble on about lists and arrays in F#. Coming from a C# background I barely ever use arrays now days in my C# code – why you may ask – because I find lists generally handle most of the business scenario’s that I come across. So it has been an interesting experience with me keep bumping into Array’s & Lists in F# and I wondered why the frequency of coming across arrays was so much more in this language than in C#. Take for instance ......

Posted On Friday, June 4, 2010 5:19 PM

My Optimized Adam & Eve
Today I had a few minutes in the evening to go over my original Adam and Eve code… what I wanted to see tonight was if I could optimize the code any further… which I was pretty sure could be done. Ultimately what I wanted to find from the experiment was a balance between optimized code an reusable code. On the one hand I can put everything into a single function and end up with a totally unusable function that is extremely compressed, which would have big comebacks when making modifications at a ......

Posted On Friday, June 4, 2010 5:19 PM

Expert F# – Pattern Matching with Adam and Eve
So I am loving my Expert F# book. I wish I had more time with it, but the little time I get I really enjoy. However today I was completely stumped by what the book was trying to get across with regards to pattern matching. On Page 38 – Chapter 3, it briefly describes F# option values. On this page it gives the code snippet along the code lines below and then goes on to speak briefly about pattern matching... open System type 'a option = | None | Some of 'a let people = [ ("Adam", None); ("Eve", None); ......

Posted On Tuesday, June 1, 2010 6:07 PM

Copyright © MarkPearl | Powered by: GeeksWithBlogs.net | Join free