Table Of ContentLearning Haskell Data Analysis
Analyze, manipulate, and process datasets of varying
sizes efficiently using Haskell
James Church
BIRMINGHAM - MUMBAI
Learning Haskell Data Analysis
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: May 2015
Production reference: 1250515
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-470-7
www.packtpub.com
Credits
Author Project Coordinator
James Church Milton Dsouza
Reviewers Proofreaders
Joseph Adams Stephen Copestake
William Kong Safis Editing
Samuli Thomasson
Indexer
Priya Sane
Commissioning Editor
Ashwin Nair
Graphics
Sheetal Aute
Acquisition Editor
Shaon Basu Jason Monteiro
Abhinash Sahu
Content Development Editor
Parita Khedekar
Production Coordinator
Shantanu N. Zagade
Technical Editor
Gaurav Suri
Cover Work
Shantanu N. Zagade
Copy Editors
Aditya Nair
Vedangi Narvekar
About the Author
James Church is an assistant professor of computer science at the University of
West Georgia. James completed his PhD in computer science from the University
of Mississippi under the advisement of Dr. Yixin Chen, with a research focus on
computational geometry.
While at the University of Mississippi, he learned the skills necessary for data
analysis in his side job, where he worked as a database administrator and analyst
for the Marijuana Potency Monitoring Program (MPMP) led by Dr. Mahmoud
ElSohly. The software written by James is used by the laboratory to store and track
the chemical composition of marijuana samples. This data is provided to the United
States National Institute on Drug Abuse to report marijuana potency.
The knowledge gained through his experience as an analyst for the MPMP
(as well as other companies) was turned into a data analysis course for
undergraduates at the University of Mississippi. This course was taught
using the languages of Python and R.
James enjoys spending time with his wife, Michelle Allen, teaching, and playing
board games with his friends.
I would like to thank the reviewers for testing and reshaping the
code in these pages into something readable. While I made every
attempt to write the best possible book on this topic, I'm sure that
mistakes will be found. All mistakes are my own.
I would like to thank Michelle Allen. Your support made this
book possible (Michelle drew the sketch of the airplane found in
Chapter 8, Building a Recommendation Engine).
I would like to thank Dr. Yixin Chen, Dr. Dawn Wilkins, Dr. Conrad
Cunningham, and the faculty of computer and information science
at the University of Mississippi.
Finally, thanks, mom and dad.
About the Reviewers
Joseph Adams is a keen programmer, writing in Go, Haskell, Python, Java,
and a few more programming languages. His interests include the designing of
programming languages, especially the functional and esoteric ones, as well as the
application of unusual data structures. When not programming, he enjoys reading
books ranging from topics such as philosophy to logic and advanced mathematics.
Joseph also regularly attends international computer conferences and has even
spoken on his Scheme to Go compiler at Free and Open Source Software
Developers' European Meeting (FOSDEM).
He has a number of public repositories on GitHub that you should definitely
check out by visiting http://github.com/jcla1. He also blogs quite regularly
at http://jcla1.com.
William Kong studied at the University of Waterloo, Canada, and holds a bachelor
of mathematics degree with a specialization in mathematical finance and a minor
in statistics. He has an extensive experience in the Haskell and R programming
languages and has worked as a SAS statistical programmer and modeler in the
financial risk modeling industry.
William's concentrations have been mainly in the fields of optimization, statistics,
and computational mathematics. His current interests lie in the practical application
of novel programming paradigms and languages, such as the ever-functional Haskell
and the latest newcomer, Julia.
My gratitude goes out to my parents for their support and
understanding during my long hours at my computer, reviewing
and testing code. I must also acknowledge instrumental help from
Learn You a Haskell for Great Good: A Beginner's Guide, No Starch Press
and Real World Haskell, O'Reilly Media, which were an invaluable
source of knowledge throughout the editing process.
Samuli Thomasson is a Haskell enthusiast who has written software in Haskell
for over 3 years, mostly as a hobby and partly for his studies and work. He has built
multiple web applications and miscellaneous tools, many of which were written in
Haskell. He is currently interested in functional programming, distributed systems,
data science, mathematics, and philosophy. He is a student at the Department of
Computer Science in the University of Helsinki, and he also works in the Finnish
software industry. While pursuing his budding career, he is actively searching for
the best topics to learn and specialize in.
He lives in Helsinki, Finland, with his friends. You can take a look at his website by
visiting http://funktionaali.com.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.
com and as a print book customer, you are entitled to a discount on the eBook copy.
Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign
up for a range of free newsletters and receive exclusive discounts and offers on Packt
books and eBooks.
TM
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view 9 entirely free books. Simply use your login credentials for
immediate access.
Table of Contents
Preface vii
Chapter 1: Tools of the Trade 1
Welcome to Haskell and data analysis! 1
Why Haskell? 3
Getting ready 5
Installing the Haskell platform on Linux 5
The software used in addition to Haskell 7
SQLite3 7
Gnuplot 7
LAPACK 8
Nearly essential tools of the trade 8
Version control software – Git 8
Tmux 10
Our first Haskell program 11
Interactive Haskell 15
An introductory problem 16
Summary 18
Chapter 2: Getting Our Feet Wet 19
Type is king – the implications of strict types in Haskell 19
Computing the mean of a list 20
Computing the sum of a list 20
Computing the length of a list 21
Attempting to compute the mean results in an error 21
Introducing the Fractional class 21
The fromIntegral and realToFrac functions 22
Creating our average function 22
The genericLength function 23
Metadata is just as important as data 24
[ i ]