Learning F# Using Project Euler

I’ve decided that immersion is the best way for me to learn new programming concepts. My exposure to WPF came 4 years ago when I started at a new employer and was told that my first assignment was to learn the application codebase and create a WPF Datagrid that could display data from our application (the view of the data was dynamically composed using metadata from an EAV database). Needless to say, the effort helped bring me up to speed quickly with both the internal codebase and WPF.

I want to learn F# and have been looking for a way to immerse myself in it. Enter Project Euler, an online collection of math/computer science puzzles. I’m using F# to solve the problems one by one, purposefully using the functional aspects of the language rather than the OO aspects. Here is my solution for the first problem in F#

let list =
    [1..999]
    |>List.map(fun n ->
               if n % 3 = 0 || n % 5=0 then
                n
               else
                0)
    |>List.sum

printfn "Sum of integers %d" list

I’m trying to figure if there’s a way for me to remove the if…then…else clause (possibly using pattern matching). But I guess it’s a good start for now.

Published Friday, May 14, 2010 8:29 PM by Mike Brown

Comments

# re: Learning F# Using Project Euler

If then is a shorter version for the below pattern matching

let list =

       [1..999]

       |>List.map(fun x -> match x % 3 = 0 || x % 5=0 with

                           |true -> x

                           |_ -> 0)

       |>List.sum

printfn "Sum of integers %d" list

Friday, May 14, 2010 5:46 PM by Naveen

# Twitter Trackbacks for Learning F# Using Project Euler - Brownie Points [azurecoding.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Learning F# Using Project Euler - Brownie Points         [azurecoding.net]        on Topsy.com

# re: Learning F# Using Project Euler

seq { 1..999 } |> Seq.filter (fun x -> x % 3 = 0 || x % 5 = 0) |> Seq.sum

Monday, May 17, 2010 1:19 AM by Yusuf Motara

# re: Learning F# Using Project Euler

Fellow F# learner here.

The example on 5/17 works, but it will stack overflow if you try [0..999999].

I wonder why... trying to think of a recursive function that would do this. I'm sure it'd be faster than the iteration it has to do on 1..999999.

Wednesday, October 20, 2010 1:20 PM by Gerardo

Leave a Comment

(required) 
(required) 
(optional)
(required)