Haskell Tutorial and Cookbook

Total Page:16

File Type:pdf, Size:1020Kb

Haskell Tutorial and Cookbook Haskell Tutorial and Cookbook Mark Watson This book is for sale at http://leanpub.com/haskell-cookbook This version was published on 2021-02-14 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2016 - 2021 Mark Watson Contents Cover Material, Copyright, and License ................................ 1 Preface ...................................................... 2 Additional Material in the Second Edition .............................. 2 A Request from the Author ....................................... 2 Structure of the Book .......................................... 3 Code Examples .............................................. 4 Functional Programming Requires a Different Mind Set ..................... 4 eBooks Are Living Documents ..................................... 4 Setting Up Your Development Environment ............................ 5 Why Haskell? ............................................... 6 Enjoy Yourself ............................................... 7 Acknowledgements ........................................... 7 Section 1 - Tutorial .............................................. 8 Tutorial on Pure Haskell Programming ................................. 9 Interactive GHCi Shell .......................................... 9 Introduction to Haskell Types ..................................... 17 Functions Are Pure ............................................ 21 Using Parenthesis or the Special $ Character and Operator Precedence . 23 Lazy Evaluation .............................................. 25 Understanding List Comprehensions ................................. 26 Haskell Rules for Indenting Code ................................... 28 Understanding let and where ..................................... 29 Conditional do Expressions and Anonymous Functions ..................... 30 Maps .................................................... 36 Sets ..................................................... 37 More on Functions ............................................ 38 Comments on Dealing With Immutable Data and How to Structure Programs . 40 Error Handling .............................................. 41 Testing Haskell Code ........................................... 42 Pure Haskell Wrap Up .......................................... 45 Tutorial on Impure Haskell Programming ............................... 46 CONTENTS Hello IO () Monad ............................................ 46 A Note About >> and >>= Operators ................................. 49 Console IO Example with Stack Configuration ........................... 51 File IO ................................................... 54 Error Handling in Impure Code .................................... 56 Network IO ................................................ 58 A Haskell Game Loop that Maintains State Functionally ..................... 61 A More Detailed Look at Monads ................................... 63 Using Applicative Operators <$> and <*>: Finding Common Words in Files . 65 List Comprehensions Using the do Notation ............................ 68 Dealing With Time ............................................ 69 Using Debug.Trace ............................................ 70 Wrap Up .................................................. 72 Section 2 - Cookbook ............................................ 73 Text Processing ................................................ 74 CSV Spreadsheet Files .......................................... 74 JSON Data ................................................. 76 Cleaning Natural Language Text ................................... 78 Natural Language Processing Tools ................................... 81 Resolve Entities in Text to DBPedia URIs .............................. 82 Bag of Words Classification Model .................................. 87 Text Summarization ........................................... 92 Part of Speech Tagging ......................................... 94 Natural Language Processing Wrap Up ............................... 98 Linked Data and the Semantic Web ................................... 99 The SPARQL Query Language .....................................100 A Haskell HTTP Based SPARQL Client ...............................101 Querying Remote SPARQL Endpoints ................................103 Linked Data and Semantic Web Wrap Up ..............................106 Web Scraping ................................................. 107 Using the Wreq Library .........................................107 Using the HandsomeSoup Library for Parsing HTML . 111 Web Scraping Wrap Up .........................................113 Using Relational Databases ........................................ 114 Database Access for Sqlite .......................................114 Database Access for Postgres ......................................115 Haskell Program to Play the Blackjack Card Game ......................... 120 CONTENTS Section 3 - Larger Projects ......................................... 131 Knowledge Graph Creator ......................................... 132 Code Layout For the KGCreator Project and strategies for sharing Haskell code between projects ..............................................134 The Main Event: Detecting Entities in Text .............................136 Utility Code for Generating RDF ...................................138 Utility Code for Generating Cypher Input Data for Neo4J . 145 Top Level API Code for Handling Knowledge Graph Data Generation . 150 Wrapup for Automating the Creation of Knowledge Graphs . 152 Hybrid Haskell and Python Natural Language Processing ..................... 153 Example Use of the Haskell NLP Client ...............................153 Setting up the Python NLP Server ...................................153 Understanding the Haskell NLP Client Code ............................154 Wrapup for Using the Python SpaCy NLP Service . 156 Hybrid Haskell and Python For Coreference Resolution ...................... 157 Installing the Python Coreference Server ..............................157 Understanding the Haskell Coreference Client Code . 158 Wrapup for Using the Python Coreference NLP Service . 160 Book Wrap Up ................................................. 161 Appendix A - Haskell Tools Setup .................................... 162 stack .....................................................162 Emacs Setup ................................................163 Do you want more of an IDE-like Development Environment? . 163 hlint .....................................................163 Cover Material, Copyright, and License Copyright 2016 Mark Watson. All rights reserved. This book may be shared using the Creative Commons “share and share alike, no modifications, no commercial reuse” license. This eBook will be updated occasionally so please periodically check the leanpub.com web page for this book¹ for updates. Please visit the author’s website². If you found a copy of this book on the web and find it of value then please consider buying a copy at leanpub.com/haskell-cookbook³ to support the author and fund work for future updates. ¹https://leanpub.com/haskell-cookbook ²http://markwatson.com ³https://leanpub.com/haskell-cookbook Preface This is the preface to the new second edition released summer of 2019. It took me over a year learning Haskell before I became comfortable with the language because I tried to learn too much at once. There are two aspects to Haskell development: writing pure functional code and writing impure code that needs to maintain state and generally deal with the world non- deterministically. I usually find writing pure functional Haskell code to be easy and a lot of fun. Writing impure code is sometimes a different story. This is why I am taking a different approach to teaching you to program in Haskell: we begin techniques for writing concise, easy to read and understand efficient pure Haskell code. I will then show you patterns for writing impure code to deal with file IO, network IO, database access, and web access. You will see that the impure code tends to be (hopefully!) a small part of your application and is isolated in the impure main program and in a few impure helper functions used by the main program. Finally, we will look at a few larger Haskell programs. Additional Material in the Second Edition In addition to updating the introduction to Haskell and tutorial material, I have added a few larger projects to the second edition. The project knowledge_graph_creator helps to automate the process of creating Knowledge Graphs from raw text input and generates data for both the Neo4J open source graph database as well as RDF data for use in semantic web and linked data applications. The project HybridHaskellPythonNlp is a hybrid project: a Python web service that provides access to the SpaCy natural language processing (NLP) library and select NLP deep learning models and a Haskell client for accessing this service. It sometimes makes sense to develop polyglot applications (i.e., applications written in multiple programming languages) to take advantage of language specific libraries and frameworks. We will also use a similar hybrid example HybridHaskellPythonCore- fAnaphoraResolution that uses another deep learning model to replace pronouns in text with the original nouns
Recommended publications
  • Microsoft's Internet Exploration: Predatory Or Competitive?
    Cornell Journal of Law and Public Policy Volume 9 Article 3 Issue 1 Fall 1999 Microsoft’s Internet Exploration: Predatory or Competitive Thomas W. Hazlett Follow this and additional works at: http://scholarship.law.cornell.edu/cjlpp Part of the Law Commons Recommended Citation Hazlett, Thomas W. (1999) "Microsoft’s Internet Exploration: Predatory or Competitive," Cornell Journal of Law and Public Policy: Vol. 9: Iss. 1, Article 3. Available at: http://scholarship.law.cornell.edu/cjlpp/vol9/iss1/3 This Article is brought to you for free and open access by the Journals at Scholarship@Cornell Law: A Digital Repository. It has been accepted for inclusion in Cornell Journal of Law and Public Policy by an authorized administrator of Scholarship@Cornell Law: A Digital Repository. For more information, please contact [email protected]. MICROSOFT'S INTERNET EXPLORATION: PREDATORY OR COMPETITIVE? Thomas W. Hazlettt In May 1998 the U.S. Department of Justice ("DOJ") accused Microsoft of violatirig the Sherman Antitrust Act by vigorously compet- ing against Netscape's Navigator software with Microsoft's rival browser, Internet Explorer. The substance of the allegation revolves around defensive actions taken by Microsoft to protect the dominant po- sition enjoyed by Microsoft's Windows operating system. The DOJ's theory is that, were it not for Microsoft's overly aggressive reaction to Netscape, Navigator software would have been more broadly distributed, thus enabling competition to Windows. This competition would have come directly from Java, a computer language developed by Sun Microsystems and embedded in Netscape software, allowing applications to run on any underlying operating system.
    [Show full text]
  • Important Product Information
    CIMPLICITY 11 Important Product Information GE Digital Proficy Historian and Operations Hub: Data Analysis in Context 1 Proprietary Notice The information contained in this publication is believed to be accurate and reliable. However, General Electric Company assumes no responsibilities for any errors, omissions or inaccuracies. Information contained in the publication is subject to change without notice. No part of this publication may be reproduced in any form, or stored in a database or retrieval system, or transmitted or distributed in any form by any means, electronic, mechanical photocopying, recording or otherwise, without the prior written permission of General Electric Company. Information contained herein is subject to change without notice. © 2020, General Electric Company. All rights reserved. Trademark Notices GE, the GE Monogram, and Predix are either registered trademarks or trademarks of General Electric Company. Microsoft® is a registered trademark of Microsoft Corporation, in the United States and/or other countries. All other trademarks are the property of their respective owners. We want to hear from you. If you have any comments, questions, or suggestions about our documentation, send them to the following email address: [email protected] Important Product Information Chapter 1. Important Product Information......................................................................................3 What's New in CIMPLICITY 11...................................................................................................3 System
    [Show full text]
  • Cache Files Detect and Eliminate Privacy Threats
    Award-Winning Privacy Software for OS X Every time you surf the web or use your computer, bits of Recover Disk Space data containing sensitive information are left behind that Over time, the files generated by web browsers can start could compromise your privacy. PrivacyScan provides to take up a large amount of space on your hard drive, protection by scanning for these threats and offers negatively impacting your computer’s performance. multiple removal options to securely erase them from PrivacyScan can locate and removes these space hogs, your system. freeing up valuable disk space and giving your system a speed boost in the process. PrivacyScan can seek and destroy internet files used for tracking your online whereabouts, including browsing history, cache files, cookies, search history, and more. Secure File Shredding Additionally, PrivacyScan can eliminate Flash Cookies, PrivacyScan utilizes advanced secure delete algorithms which are normally hidden away on your system. that meet and exceed US Department of Defense recommendations to ensure complete removal of Privacy Threat: Cookies sensitive data. Cookies can be used to track your usage of websites, determining which pages you visited and the length Intuitive Interface of time you spent on each page. Advertisers can use PrivacyScan’s award-winning design makes it easy to cookies to track you across multiple sites, building up track down privacy threats that exist on your system and a “profile” of who you are based on your web browsing quickly eliminate them. An integrated setup assistant and habits. tip system provide help every step of the way to make file cleaning a breeze.
    [Show full text]
  • Definition and Methods for the Carbon Handprint of Buildings
    Definition and methods for the carbon handprint of buildings 8.2.2021 Authors: Tarja Häkkinen [email protected] Sylviane Nibel [email protected] Harpa Birgisdottir [email protected] 2 Table of contents 1 Background ..................................................................................................................... 4 2 Objectives........................................................................................................................ 7 3 Methods and execution of work ...................................................................................... 8 Study of literature ................................................................................................................... 8 Study of possible handprint cases for buildings ..................................................................... 9 Study of relevant standards .................................................................................................. 10 4 Study of literature.......................................................................................................... 11 Definitions and approaches .................................................................................................. 11 Handprint thinking................................................................................................................ 13 Needs and barriers for the handprint concept ..................................................................... 14 Measures that improve carbon handprint ..........................................................................
    [Show full text]
  • Free Software an Introduction
    Free Software an Introduction By Steve Riddett using Scribus 1.3.3.12 and Ubuntu 8.10 Contents Famous Free Software...................................................... 2 The Difference.................................................................. 3 Stallman and Torvalds.......................................................4 The Meaning of Distro......................................................5 Linux and the Holy Grail.................................................. 6 Frequently Asked Questions............................................. 7 Where to find out more.....................................................8 2 Free Software - an Introduction Famous Free Software Firefox is a web browser similar to Microsoft's Internet Explorer but made the Free software way. The project started in 2003 from the source code of the Netscape browser which had been released when Netscape went bust. In April 2009, Firefox recorded 29% use worldwide (34% in Europe). Firefox is standards compliant and has a system of add-ons which allow innovative new features to be added by the community. OpenOffice.org is an office suite similar to Microsoft Office. It started life as Star Office. Sun Microsystems realised it was cheaper to buy out Star Office than to pay Microsoft for licence fees for MS Office. Sun then released the source code for Star Office under the name OpenOffice.org. OpenOffice.org is mostly compatible with MS Office file formats, which allows users to open .docs and .xls files in Open Office. Microsoft is working on a plug-in for MS Office that allows it to open .odf files. ODF (Open Document Format) is Open Office's default file format. Once this plug-in is complete there will 100% compatiblity between the two office suites. VLC is the VideoLAN Client. It was originally designed to allow you to watch video over the network.
    [Show full text]
  • AIR Forum 2017
    COVER 2 COVER 3 Increase the productivity of your institution with smart tools that fuel innovation, opportunity and discovery. See greater value in your data. SAS® Visual Analytics for institutional research Capture, understand and Monitor the reach Store, share and Get actionable showcase the outcomes & infuence of your manage all of your funding insights for Quickly see through the complexity to fi nd of your research scholarly content research outputs better decisions patterns, trends, key relationships and potential outcomes. And then turn your institutional data into much more – a valued asset. Try it for yourself Work smart. sas.com/air17 Discover more. © 2014 SAS Institute Inc. All rights reserved. S123813US.0214 SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. Other brand and product names are trademarks of their respective companies. © 2016 SAS Institute Inc. All rights reserved. S152264US.0316 ON BOOTHVISIT 215 US Using Data to Equip your faculty and staff Improve Student with the tools they need to make data-driven decisions. Outcomes Visualize and analyze data and spot the early indicators and interventions. Helping universities improve through performance analysis and benchmarking Guide your leadership to respond through shared reports viewable online Our new suite of data products allows you to compare your university to others, helping you to identify strategic partners through a variety of devices. and potential competitors. Learn how Stetson University With THEDataPoints™ you can access all the tools you need to understand your institution’s individual performance.
    [Show full text]
  • Ancient Political Theory
    ANCIENT POLITICAL THEORY HIST 2100, PHIL 2060, and POLS 2300, Fall 2013, TR 12.00-1.15 PM Charlie McAllister, ADM 334, 704/637-4344 (O) and 336/287-4403 (C) [email protected] DESCRIPTION -- "A critical analysis of the basic political writings of selected modern authors important to the development of western civilization, e.g., Plato, Aristotle, St. Augustine and St. Thomas" Catawba College Catalog). These crucial thinkers -- along with classical Greek playwrights and the Italian Renaissance philosopher Machiavelli -- will help us seek answers to three simple questions: 1) What is a good person? 2) What is a good life? and 3) What is a good society? WEBBOOKS -- http://faculty.catawba.edu/cmcallis/history/history.htm CORE -- For the second part of our syllabus (Connecting) and other resources, visit our WebBooks page. TEXTS Peter J. Steinberger, ed., Readings in Classical Political Thought (Hackett, 2000) [RCPT] Aristotle, Nicomachean Ethics, trans. Terrence Irwin (Hackett, 2e, 1999) Edward Bryan Portis, Reconstructing the Classics: Political Theory from Plato to Marx (2e, 1998): free at EPBCO Host through our library William Strunk, Jr. and E. B. White, Elements of Style (Allyn & Bacon, 4e, 1999) A collegiate dictionary, thesaurus, and Bible ABBREVIATIONS BBD = Blackboard Document RCPT = Steinberger Z = Zinger E = Thematic essay S = Summary of a scholarly article ***WEB RESOURCES DAILY*** DATE DAY DAILY ASSIGNMENTS DUE AUG 22 R CLASSICAL POLITICAL PHILOSOPHY 27 T EARLY FOLKS: Poets, Presocratics, and Sophists -- APT Connecting [BBD], RCPT xi-28, and Portis 1 (Great Books and Political Science). 29 R HISTORIANS, I: Herodotus -- RCPT 29-30 and 32-35, and any one S relevant scholarly article on our WebBook page.
    [Show full text]
  • The Elinks Manual the Elinks Manual Table of Contents Preface
    The ELinks Manual The ELinks Manual Table of Contents Preface.......................................................................................................................................................ix 1. Getting ELinks up and running...........................................................................................................1 1.1. Building and Installing ELinks...................................................................................................1 1.2. Requirements..............................................................................................................................1 1.3. Recommended Libraries and Programs......................................................................................1 1.4. Further reading............................................................................................................................2 1.5. Tips to obtain a very small static elinks binary...........................................................................2 1.6. ECMAScript support?!...............................................................................................................4 1.6.1. Ok, so how to get the ECMAScript support working?...................................................4 1.6.2. The ECMAScript support is buggy! Shall I blame Mozilla people?..............................6 1.6.3. Now, I would still like NJS or a new JS engine from scratch. .....................................6 1.7. Feature configuration file (features.conf).............................................................................7
    [Show full text]
  • HTTP Cookie - Wikipedia, the Free Encyclopedia 14/05/2014
    HTTP cookie - Wikipedia, the free encyclopedia 14/05/2014 Create account Log in Article Talk Read Edit View history Search HTTP cookie From Wikipedia, the free encyclopedia Navigation A cookie, also known as an HTTP cookie, web cookie, or browser HTTP Main page cookie, is a small piece of data sent from a website and stored in a Persistence · Compression · HTTPS · Contents user's web browser while the user is browsing that website. Every time Request methods Featured content the user loads the website, the browser sends the cookie back to the OPTIONS · GET · HEAD · POST · PUT · Current events server to notify the website of the user's previous activity.[1] Cookies DELETE · TRACE · CONNECT · PATCH · Random article Donate to Wikipedia were designed to be a reliable mechanism for websites to remember Header fields Wikimedia Shop stateful information (such as items in a shopping cart) or to record the Cookie · ETag · Location · HTTP referer · DNT user's browsing activity (including clicking particular buttons, logging in, · X-Forwarded-For · Interaction or recording which pages were visited by the user as far back as months Status codes or years ago). 301 Moved Permanently · 302 Found · Help 303 See Other · 403 Forbidden · About Wikipedia Although cookies cannot carry viruses, and cannot install malware on 404 Not Found · [2] Community portal the host computer, tracking cookies and especially third-party v · t · e · Recent changes tracking cookies are commonly used as ways to compile long-term Contact page records of individuals' browsing histories—a potential privacy concern that prompted European[3] and U.S.
    [Show full text]
  • Moregoodnews
    (f/ MONDAY, SEPTEMBER 26, 1966 FAOe TWENTY-fKaUl Average OaUy Net Press Run The Weather For._thijVeek Ended Cloudy, not ee cool toalgbt, and Bchnier, Grlgalls, Pllukas, Septcndwr M, 1966 low 80-05; pertly cloudy tomer* Dedication Reset 4 Requests and ’Tunsl^ properties. This W eek About Town 2. Roger Ricard, to change z w , U gb in 60e. The dedication of Man­ to Industrial Zone aU or part 14,757 MMnben o f the Professional chester’s new Senior Citi­ Before T P C of a parcel bounded by HUliard Manchester^A City of Village Charm . Women’s Club wrlll have a pot- zens’ Center, originally St., Adams St. and the Hocka- luck tom om w night at 6:30 at (CleMifled /Mworttolng OB Psge 91) PRICE SEVEN CENTi scheduled for Wednesday, The Town Planning Commis­ num River, and now in Res­ VOL. LXXXV, NO* 304 (TWENTY-FOUR PAGES—TWO SECTIONS) MANCHESTER, CONN., TUESDAY, SEPTEMBER 27, 1966 Center Church. There will be a has been set ahead one day sion (’TPC) will conduct pub­ idence Zone A. The TPC has program of vacation reminis­ to Thursday at 2 p.m. lic hearings tonight on one sub­ extended Ricard’s appUcation to cences. Hostesses are Miss Syl­ The dedication, open to division proposal and on three include the Bezzini and Gerich via Claflln, Miss Helen Carrier, zone-change requests. properties. Miss Mabel Trotter, Miss Ethel the public, will be witnessed by local and state officials Goslee and Mrs. Edwin J. Don­ ’The session will be at 8 in the 2.
    [Show full text]
  • Share Brother Printer DCP-1610W with Linux CUPS and Samba Windows Share
    ? Walking in Light with Christ - Faith, Computing, Diary Linux, UNIX, FreeBSD, Windows, Mac OS - Hacks, Goodies, Tips and Tricks and The True Meaning of life http://www.pc-freak.net/blog Enable printing from Windows and Macs remotely through Linux Print server - Share Brother Printer DCP-1610W with Linux CUPS and Samba Windows Share Author : admin I've recently bought a new Printer model Brother DCP 1610W and as in my home I have already a small Linux router and a web server where this blog and a couple of other websites runs and I need multiple PC / notebook / mobile phone enabled people to print on the Printer easily pretty much like a Printing server for a Small Office environment. To do that of course I needed it configured to be accessible remotely for print via LAN and Wireless network. The task is not a complex one and printing remotely over the network is a standard thing many company organizations / universities and univerities does for quite some time and hence nowadays most printers are network connect ready so you just have to place them inside your home or corporate network and use the time to configure them via their web configuration interface or even some have their own embedded wifi adapter, as well as many printers nowdays can even be ready to print directly by just 1 / 17 ? Walking in Light with Christ - Faith, Computing, Diary Linux, UNIX, FreeBSD, Windows, Mac OS - Hacks, Goodies, Tips and Tricks and The True Meaning of life http://www.pc-freak.net/blog connecting the Printer to the Wi-Fi network and installing its drivers on a Win host.
    [Show full text]
  • IBM Lotus Notes/Domino Next - New Ways to Work …
    Уффе Соренсен IBM Lotus Notes/Domino Next - New ways to work … @uffesorensen usorensen #ibmsocbiz ● Major new release of all components ● Notes / Domino / iNotes 8.5.4 - the “Social Edition” ­ Public Beta (based on Code Drop 6) in mid-Nov 2012 ­ Target General Availability 1q2013 Notes and Domino software The flexible and comprehensive collaboration solution & application platform The Users: The Servers: ● Notes (Mac, Linux, Windows) ● Domino ● ● iNotes Universal access IBM XWork ● Notes Traveler ● Internet Explorer Remain productive regardless of location ● Firefox ● Safari Browser ● iOS Open application ● Android development ● Rich Nokia clients Fully extensible, standards-based, Web 2.0 / OpenSocial 2.0, XPages Advanced Collaboration collaboration foundation Collaboration capabilities Mobile E-mail, calendar, in context contacts, Instant messaging, user profiles Seamless, file sharing, office uninterrupted workflow, productivity tools activity stream Proven, reliable & scalable infrastructure Security-rich, high availability, simple upgrades ©2012 IBM Corporation The Notes rich client: Your “everything working together” - in one place ... Social Networks: Social File Activities, Blogs, Wikis, .. Sharing Standard Instant Web Browser Messaging of the operating system Documents, Feeds, Presentations, My Widgets, Spreadsheets Live Text Compositions / mashups of E-Mail, collaborative and Calendar, Contacts Business Apps One intuitive UI as the central place where everything integrates in context ©2012 IBM Corporation Lotus Notes / Domino
    [Show full text]