Get Programming with Haskell
eBook - ePub

Get Programming with Haskell

Will Kurt

Condividi libro
  1. 616 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Get Programming with Haskell

Will Kurt

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

Summary
Get Programming with Haskell leads you through short lessons, examples, and exercises designed to make Haskell your own. It has crystal-clear illustrations and guided practice. You will write and test dozens of interesting programs and dive into custom Haskell modules. You will gain a new perspective on programming plus the practical ability to use Haskell in the everyday world. (The 80 IQ points: not guaranteed.) Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology
Programming languages often differ only around the edges—a few keywords, libraries, or platform choices. Haskell gives you an entirely new point of view. To the software pioneer Alan Kay, a change in perspective can be worth 80 IQ points and Haskellers agree on the dramatic benefits of thinking the Haskell way—thinking functionally, with type safety, mathematical certainty, and more. In this hands-on book, that's exactly what you'll learn to do. What's Inside

  • Thinking in Haskell
  • Functional programming basics
  • Programming in types
  • Real-world applications for Haskell


About the Reader
Written for readers who know one or more programming languages. Table of Contents
Lesson 1 Getting started with Haskell
Unit 1 - FOUNDATIONS OF FUNCTIONAL PROGRAMMING
Lesson 2 Functions and functional programming
Lesson 3 Lambda functions and lexical scope
Lesson 4 First-class functions
Lesson 5 Closures and partial application
Lesson 6 Lists
Lesson 7 Rules for recursion and pattern matching
Lesson 8 Writing recursive functions
Lesson 9 Higher-order functions
Lesson 10 Capstone: Functional object-oriented programming with robots!
Unit 2 - INTRODUCING TYPES
Lesson 11 Type basics
Lesson 12 Creating your own types
Lesson 13 Type classes
Lesson 14 Using type classes
Lesson 15 Capstone: Secret messages!
Unit 3 - PROGRAMMING IN TYPES
Lesson 16 Creating types with "and" and "or"
Lesson 17 Design by composition—Semigroups and Monoids
Lesson 18 Parameterized types
Lesson 19 The Maybe type: dealing with missing values
Lesson 20 Capstone: Time series
Unit 4 - IO IN HASKELL
Lesson 21 Hello World!—introducing IO types
Lesson 22 Interacting with the command line and lazy I/O
Lesson 23 Working with text and Unicode
Lesson 24 Working with files
Lesson 25 Working with binary data
Lesson 26 Capstone: Processing binary files and book data
Unit 5 - WORKING WITH TYPE IN A CONTEXT
Lesson 27 The Functor type class
Lesson 28 A peek at the Applicative type class: using functions in a context
Lesson 29 Lists as context: a deeper look at the Applicative type class
Lesson 30 Introducing the Monad type class
Lesson 31 Making Monads easier with donotation
Lesson 32 The list monad and list comprehensions
Lesson 33 Capstone: SQL-like queries in Haskell
Unit 6 - ORGANIZING CODE AND BUILDING PROJECTS
Lesson 34 Organizing Haskell code with modules
Lesson 35 Building projects with stack
Lesson 36 Property testing with QuickCheck
Lesson 37 Capstone: Building a prime-number library
Unit 7 - PRACTICAL HASKELL
Lesson 38 Errors in Haskell and the Either type
Lesson 39 Making HTTP requests in Haskell
Lesson 40 Working with JSON data by using Aeson
Lesson 41 Using databases in Haskell
Lesson 42 Efficient, stateful arrays in Haskell
Afterword - What's next?
Appendix - Sample answers to exercise

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Get Programming with Haskell è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Get Programming with Haskell di Will Kurt in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Computer Science e Programming Languages. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Editore
Manning
Anno
2018
ISBN
9781638356776

Lesson 1. Getting started with Haskell

After reading lesson 1, you’ll be able to
  • Install tools for Haskell development
  • Use GHC and GHCi
  • Use tips for writing Haskell programs

1.1. Welcome to Haskell

Before you dive into learning Haskell, you need to become familiar with the basic tools you’ll be using on your journey. This lesson walks you through getting started with Haskell. The lesson starts with downloading the basics to write, compile, and run Haskell programs. You’ll then look at example code and start thinking about how to write code in Haskell. After this lesson, you’ll be ready to dive in!

1.1.1. The Haskell Platform

The worst part of learning a new programming language is getting your development environment set up for the first time. Fortunately, and somewhat surprisingly, this isn’t a problem at all with Haskell. The Haskell community has put together a single, easily installable package of useful tools referred to as the Haskell Platform. The Haskell Platform is the “batteries included” model of packaging a programming language.
The Haskell Platform includes the following:
  • The Glasgow Haskell Compiler (GHC)
  • An interactive interpreter (GHCi)
  • The stack tool for managing Haskell projects
  • A bunch of useful Haskell packages
The Haskell Platform can be downloaded from www.haskell.org/downloads#platform. From there, follow the directions for installing on your OS of choice. This book uses Haskell version 8.0.1 or higher.

1.1.2. Text editors

Now that you have the Haskell Platform installed, you’re probably curious about which editor you should use. Haskell is a language that strongly encourages you to think before you hack. As a result, Haskell programs tend to be extremely terse. There’s little that an editor can do for you, other than manage indentation and provide helpful syntax highlighting. Many Haskell developers use Emacs with haskell-mode. But if you’re not already familiar with Emacs (or don’t like to work with it), it’s certainly not worth the work to learn Emacs in addition to Haskell. My recommendation is that you look for a Haskell plugin for whatever editor you use the most. A bare-bones text editor, such as Pico or Notepad++, will work just fine for this book, and most full-fledged IDEs have Haskell plugins.

1.2. The Glasgow Haskell Compiler

Haskell is a compiled language, and the Glasgow Haskell Compiler is the reason Haskell is as powerful as it is. The job of the compiler is to transform human-readable source code into machine-readable binary. At the end compilation, you’re left with an executable binary file. This is different from when you run Ruby, for example, in which another program reads in your source code and interprets it on the fly (this is accomplished with an interpreter). The main benefit of a compiler over an interpreter is that because the compiler transforms code in advance, it can perform analysis and optimization of the code you’ve written. Because of some other design features of Haskell, namely its powerful type system, there’s an adage that if it compiles, it works. Though you’ll use GHC often, never take it for granted. It’s an amazing piece of software in its own right.
To invoke GHC, open a terminal and type in ghc:
$ ghc
In this text, whenever you come across a $ sign, it means you’re typing into a command prompt. Of course, with no file to compile, GHC will complain. To get started, you’ll make a simple file called hello.hs. In your text editor of choice, create a new file named hello.hs and enter the following code.
Listing 1.1. hello.hs a Hello World program
--hello.hs my first Haskell file! 1 main = do 2 print "Hello World!" 3
  • 1 A commented line with the name of your file
  • 2 The start of your ‘main’ function
  • 3 The main function prints out “Hello World”
At this point, don’t worry too much about what’s happening in any of the code in this section. Your real aim here is to learn the tools you need so that they don’t get in the way while you’re learning Haskell.
Now that you have a sample file, you can run GHC again, this time passing in your hello.hs file as an argument:
$ ghc hello.hs [1 of 1] Compiling Main Linking hello ...
If the compilation was successful, GHC will have created three files:
  • hello (hello.exe on Windows)
  • hello.hi
  • hello.o
Starting out, the most important file is hello, which is your binary executable. Because this file is a binary executable, you can simply run the file:
$ ./hello "Hello World!"
Notice that the default behavior of the compiled program is to execute the logic in main. By default, all Haskell programs you’re compiling need to have a main, which plays a similar role to the Main method in Java/C++/C# or __main__ in Python.
Like most command-line tools, GHC supports a wide range of optional flags. For example, if you want to compile hello.hs into an executable named helloworld, you can use the -o flag:
$ghc hello.hs -o helloword [1 of 1] Compiling Main Linking helloworld ....
For a more complete listing of compiler options, call ghc --help (no filename argument is required).
Quick check 1.1
Q1:
Copy the code for hello.hs and compile your own executable named testprogram.
QC 1.1 answer
1:
Simply copy the code to a file and then run this in the same directory as the file: ghc hello.hs -o testprogram

1.3. Interacting with Haskell—GHCi

One of the most useful tools for writing Haskell programs is GHCi, an interactive interface for GHC. Just like GHC, GHCi is started with a simple command: ghci. When you start GHCi, you’ll be greeted with a new prompt:
$ ghci GHCi>
This book indicates when you’re using GHCi by using GHCi> for lines you input and a blank for lines that are output by GHCi. The first thing to learn about any program you start from the command line is how to get out of it! For GHCi, you use the :q command to exit:
$ ghci GHCi> :q Leaving GHCi.
Working with GHCi is much like working with interpreters in most interpreted programming languages such as Python and Ruby. It can be used as a simple calculator:
GHCi> 1 + 1 2
You can also write code on the fly in GHCi:
GHCi> x = 2 + 2 GHCi> x 4
Prior to version 8 of GHCi, function and variable definitions needed to be prefaced with a let keyword. This is no longer necessary, but many Haskell examples on the web and in older books still include it:
GHCi> let f x = x + x GHCi> f 2 4
The most important use of GHCi is interacting with programs that you’re writing. There are two ways to load an existing file into GHCi. The first is to pass the filename as an argument to ghci:
$ ghci hello.hs [1 of 1] Compiling ...

Indice dei contenuti