Home > Development, F# > F# Koans – Stock Example

F# Koans – Stock Example

At Leeds Sharp the other day, we started doing the F# Koans as a way of of trying out the language, with the aim of expanding our minds with some functional programming.  A bit like coder’s LSD, if you will.

The koans are pretty good at introducing you to things, although initially it’s a bit too easy to get through them without really understanding what’s going on.

However, when you get to the stock example that all changes, as you have to actually use some of what you’ve learned to solve a more involved problem. The purpose of this post is to put up my solution  in the hope of getting into a bit of a discussion with others who’ve done it, at Leeds Sharp or otherwise. I have no doubt that my implementation could be improved, so any advice would be more than welcome!

module ``about the stock example`` =

  let stockData =
    [ "Date,Open,High,Low,Close,Volume,Adj Close";
      // ...
      "2012-02-29,31.89,32.00,31.61,31.74,59323600,31.74"; ]

  // function to split a comma-separated string into an array
  let splitOnCommas (dataAsString:string) =

  // function to take the desired parts of the array as a tuple,
  // including opening-closing difference
  let createPriceDifferenceTuple (fullList:string[]) =
    ( fullList.[0],
      abs (Double.Parse(fullList.[1]) - Double.Parse(fullList.[4])) )

  let maximumDifferenceTuple =
    // split strings into arrays, ignoring header
    List.map splitOnCommas stockData.Tail
    // map arrays to tuples
    |> List.map createPriceDifferenceTuple
    // get maximum tuple based on second element (price difference)
    |> List.maxBy snd

  let YouGotTheAnswerCorrect() =
    let result = fst maximumDifferenceTuple // get date, first item in tuple
    AssertEquality "2012-03-13" result

Categories: Development, F#
  1. April 2, 2013 at 10:34 am

    I just came across the TryFSharp site, which seems to have this same problem in it, with a solution: http://www.tryfsharp.org/Learn/getting-started#chaining-functions

    As expected, it’s better than mine! Similar, but just gets the required array directly instead of putting them into tuples first.

  2. April 12, 2013 at 3:23 pm

    I got stuck, and googled for solutions and found this page. Looks like you’ve become the definitive source for this.

    I couldn’t figure out how to get maxBy to do what I wanted it to.

  3. April 12, 2013 at 3:25 pm

    Koans seem like a great way to learn syntax, but when it came to this example I realised I’d not actually remembered much of it.

  4. relentless development
    April 12, 2013 at 3:49 pm

    Lol, it’s the blind leading the blind! Yeah I felt the same, the koans are a good starting point but (apart from this) you don’t really have to do enough to actually get it into your brain.

  5. June 25, 2013 at 5:24 am

    I just blogged about this F# Koan as well and just saw your solution. I didn’t think about using tuple at all. And my solution still doesn’t looks so-functional though..lol http://cyanbyfuchsia.wordpress.com/2013/06/25/learning-f-aboutthestockexample-koan/

  6. June 25, 2013 at 8:28 am

    Hi kimsk, as I said on your blog your solution looks pretty functional to me. If anyone’s looking for a slightly different take on the solution, go and check it out!

  7. spikej
    February 2, 2015 at 9:35 pm

    mine ended up being fairly similar: http://pastebin.com/YnbBuBEe

    I did a Google search after I’d finished to see what other answers existed

    • Grant Crofton
      February 4, 2015 at 2:45 pm

      Yes, looks good. I think yours is more readable as you’ve used the enum for the columns, and split out the double parsing into a separate function (although it’s a shame you have to cast the enum to an int).

      But they all get the job done, and that’s the main thing!

      I don’t see on yours where it actually selects the date though, doesn’t ‘result’ contain the whole record at the end?

      • spikej
        February 4, 2015 at 3:51 pm

        You’re right, I got a little stuck trying to get it to output just that one part… line 44 should have been

        |> (fun x -> x.[int dataItem.Date])

        This I got from the tryfsharp.org example

  1. June 25, 2013 at 5:16 am

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: