• Complain

Bryan OSullivan - Real World Haskell

Here you can read online Bryan OSullivan - Real World Haskell full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2008, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Bryan OSullivan Real World Haskell

Real World Haskell: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Real World Haskell" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

This easy-to-use, fast-moving tutorial introduces you to functional programming with Haskell. Youll learn how to use Haskell in a variety of practical ways, from short scripts to large and demanding applications. Real World Haskell takes you through the basics of functional programming at a brisk pace, and then helps you increase your understanding of Haskell in real-world issues like I/O, performance, dealing with data, concurrency, and more as you move through each chapter. With this book, you will: Understand the differences between procedural and functional programming Learn the features of Haskell, and how to use it to develop useful programs Interact with filesystems, databases, and network services Write solid code with automated tests, code coverage, and error handling Harness the power of multicore systems via concurrent and parallel programming Youll find plenty of hands-on exercises, along with examples of real Haskell programs that you can modify, compile, and run. Whether or not youve used a functional language before, if you want to understand why Haskell is coming into its own as a practical language in so many major organizations, Real World Haskell is the best place to start.

Bryan OSullivan: author's other books


Who wrote Real World Haskell? Find out the surname, the name of the author of the book and a list of all author's works by series.

Real World Haskell — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Real World Haskell" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Real World Haskell - image 1
Real World Haskell - image 2
Real World Haskell, 1st Edition
By Bryan O'Sullivan, Donald Bruce Stewart, John Goerzen
...............................................
Publisher: O'Reilly Media, Inc.
Pub Date: December 5, 2008
Print ISBN-13: 978-0-596-51498-3
Pages:
Slots: 2.0
This easy-to-use fast-moving tutorial introduces you to functionalprogramming - photo 3

This easy-to-use, fast-moving tutorial introduces you to functionalprogramming with Haskell. You'll learn how to use Haskell in avariety of practical ways, from short scripts to large anddemanding applications. Real World Haskell takes youthrough the basics of functional programming at a brisk pace, andthen helps you increase your understanding of Haskell in real-worldissues like I/O, performance, dealing with data, concurrency, andmore as you move through each chapter.With this book, you will:

  • Understand the differences between procedural and functionalprogramming

  • Learn the features of Haskell, and how to use it to developuseful programs

  • Interact with filesystems, databases, and network services

  • Write solid code with automated tests, code coverage, and errorhandling

  • Harness the power of multicore systems via concurrent andparallel programming

You'll find plenty of hands-on exercises, along with examples ofreal Haskell programs that you can modify, compile, and run.Whether or not you've used a functional language before, if youwant to understand why Haskell is coming into its own as apractical language in so many major organizations, Real WorldHaskell is the best place to start.



Picture 4
Safari IT Books Language Constructs Functional Programming Haskell Safari IT Books Programming Programming Programming Bryan O'Sullivan Donald Bruce Stewart John Goerzen O'Reilly Media, Inc. Real World Haskell, 1st Edition
10.1. Grayscale Files

The name of netpbm's grayscale file format is PGM (portable gray map). It is actually not one format, but two; the plain (or P2) format is encoded as ASCII, while the more common raw (P5) format is mostly binary.

A file of either format starts with a header, which in turn begins with a "magic" string describing the format. For a plain file, the string is P2 , and for raw, it's P5 . The magic string is followed by whitespace, and then by three numbers: the width, height, and maximum gray value of the image. These numbers are represented as ASCII decimal numbers, separated by whitespace.

After the maximum gray value comes the image data. In a raw file, this is a string of binary values. In a plain file, the values are represented as ASCII decimal numbers separated by single-space characters.

A raw file can contain a sequence of images, one after the other, each with its own header. A plain file contains only one image.

Picture 5

Picture 6
Safari IT Books Language Constructs Functional Programming Haskell Safari IT Books Programming Programming Programming Bryan O'Sullivan Donald Bruce Stewart John Goerzen O'Reilly Media, Inc. Real World Haskell, 1st Edition
10.2. Parsing a Raw PGM File

For our first try at a parsing function, we'll only worry about raw PGM files. We'll write our PGM parser as a pure function. It's won't be responsible for obtaining the data to parse, just for the actual parsing. This is a common approach in Haskell programs. By separating the reading of the data from what we subsequently do with it, we gain flexibility in where we take the data from.

We'll use the ByteString type to store our graymap data, because it's compact. Since the header of a PGM file is ASCII text but its body is binary, we import both the text- and binary-oriented ByteString modules:

-- file: ch10/PNM.hsimport qualified Data.ByteString.Lazy.Char8 as L8import qualified Data.ByteString.Lazy as Limport Data.Char (isSpace)

For our purposes, it doesn't matter whether we use a lazy or strict ByteString, so we've somewhat arbitrarily chosen the lazy kind.

We'll use a straightforward data type to represent PGM images:

-- file: ch10/PNM.hsdata Greymap = Greymap { greyWidth :: Int , greyHeight :: Int , greyMax :: Int , greyData :: L.ByteString } deriving (Eq)

Normally, a Haskell Show instance should produce a string representation that we can read back by calling read. However, for a bitmap graphics file, this would potentially produce huge text strings, for example, if we were to show a photo. For this reason, we're not going to let the compiler automatically derive a Show instance for us; we'll write our own and intentionally simplify it:

-- file: ch10/PNM.hsinstance Show Greymap where show (Greymap w h m _) = "Greymap " ++ show w ++ "x" ++ show h ++ " " ++ show m

Because our Show instance intentionally avoids printing the bitmap data, there's no point in writing a Read instance, as we can't reconstruct a valid Greymap from the result of show.

Here's an obvious type for our parsing function:

-- file: ch10/PNM.hsparseP5 :: L.ByteString -> Maybe (Greymap, L.ByteString)

This will take a ByteString, and if the parse succeeds, it will return a single parsed Greymap, along with the string that remains after parsing. That residual string will be available for future parses.

Our parsing function has to consume a little bit of its input at a time. First, we need to assure ourselves that we're really looking at a raw PGM file; then we need to parse the numbers from the remainder of the header; and then we consume the bitmap data. Here's an obvious way to express this, which we will use as a base for later improvements :

-- file: ch10/PNM.hsmatchHeader :: L.ByteString -> L.ByteString -> Maybe L.ByteString-- "nat" here is short for "natural number"getNat :: L.ByteString -> Maybe (Int, L.ByteString)getBytes :: Int -> L.ByteString -> Maybe (L.ByteString, L.ByteString)parseP5 s = case matchHeader (L8.pack "P5") s of Nothing -> Nothing Just s1 -> case getNat s1 of Nothing -> Nothing Just (width, s2) -> case getNat (L8.dropWhile isSpace s2) of Nothing -> Nothing Just (height, s3) -> case getNat (L8.dropWhile isSpace s3) of Nothing -> Nothing Just (maxGrey, s4) | maxGrey > 255 -> Nothing | otherwise -> case getBytes 1 s4 of Nothing -> Nothing Just (_, s5) -> case getBytes (width * height) s5 of Nothing -> Nothing Just (bitmap, s6) -> Just (Greymap width height maxGrey bitmap, s6)

This is a very literal piece of code, performing all of the parsing in one long staircase of case expressions. Each function returns the residual ByteString left over after it has consumed all it needs from its input string. We pass each residual string along to the next step. We deconstruct each result in turn, either returning Nothing if the parsing step fails, or building up a piece of the final result as we proceed. Here are the bodies of the functions that we apply during parsing (their types are commented out because we already presented them):

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Real World Haskell»

Look at similar books to Real World Haskell. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Real World Haskell»

Discussion, reviews of the book Real World Haskell and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.