Jerky and Haskell

Well this'll be something to sink your teeth into! Today I'll be show and telling a Haskell program written to calculate every* discount from Big John's Beef Jerky, a beef jerky company that sells quality jerky.

I decided to do this in Haskell for a few reasons:

1. I needed a reason to use Haskell while learning Haskell.
2. Haskell deals with numbers very well. The ability to use higher-order functions was very convenient.
3. The Chart API was pretty straight forward and produced nice charts, which you will see soon!

There were a few times where I felt like giving up, and decided instead to just put the program to the side until I felt enough motivation again to tackle it. The following code could have easily been written in Python or Ruby. Could it have been done more pragmatically? Maybe. Would Haskell win against said languages in a game of code golf? Who cares!

Pretty straight forward right? I find it interesting and sometimes unbelievable that this is just a chain of function calls. No imperative code whatsoever.

So what I did was first extract the discount data from Big John's website by hand. I didn't scrape the web pages with the program mainly because the format of the site could change at any given moment (actually they have been planning a launch of their new website layout for awhile now). Secondly parsing HTML is just a messy job.

After extracting the data, setting it up was easy. I created a list for the loyalty discounts and a piecewise-like function for the bulk discounts. From there on out it was just a few equations to calculate differences and added things up.

Lastly, the Chart library was used to cook up some visuals. Honestly I have to say it was a pain in the arse but totally worth learning how to use it. It was only painful though because the examples of the library were definitely not noob-proof (lack of type signatures, mainly).

And here's the final result (svg converted to png)!

There's much more that could be done with this too, such as calculating the optimal jerky bag combination; including taxes from your country; and shipping costs. Maybe when I'm a little more experienced in Haskell, I'll do these things. For now though I think continuing doing a variety of small projects is the way to go to really get a feel of Haskell.


Popular Posts