• Complain

Miran Lipovaca - Learn You a Haskell for Great Good!: A Guide for Beginners

Here you can read online Miran Lipovaca - Learn You a Haskell for Great Good!: A Guide for Beginners full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2011, publisher: No Starch Press, genre: Science. 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.

Miran Lipovaca Learn You a Haskell for Great Good!: A Guide for Beginners
  • Book:
    Learn You a Haskell for Great Good!: A Guide for Beginners
  • Author:
  • Publisher:
    No Starch Press
  • Genre:
  • Year:
    2011
  • Rating:
    3 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 60
    • 1
    • 2
    • 3
    • 4
    • 5

Learn You a Haskell for Great Good!: A Guide for Beginners: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Learn You a Haskell for Great Good!: A Guide for Beginners" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Its all in the name: Learn You a Haskell for Great Good! is a hilarious, illustrated guide to this complex functional language. Packed with the authors original artwork, pop culture references, and most importantly, useful example code, this book teaches functional fundamentals in a way you never thought possible.Youll start with the kid stuff: basic syntax, recursion, types and type classes. Then once youve got the basics down, the real black belt master-class begins: youll learn to use applicative functors, monads, zippers, and all the other mythical Haskell constructs youve only read about in storybooks.As you work your way through the authors imaginative (and occasionally insane) examples, youll learn to: Laugh in the face of side effects as you wield purely functional programming techniques Use the magic of Haskells laziness to play with infinite sets of data Organize your programs by creating your own types, type classes, and modules Use Haskells elegant input/output system to share the genius of your programs with the outside world Short of eating the authors brain, you will not find a better way to learn this powerful language than reading Learn You a Haskell for Great Good! Excerpt from the Introduction Haskell is fun, and thats what its all about! This book is aimed at people who have experience programming in imperative languagessuch as C++, Java, and Pythonand now want to try out Haskell. But even if you dont have any significant programming experience, Ill bet a smart person like you will be able to follow along and learn Haskell. My first reaction to Haskell was that the language was just too weird. But after getting over that initial hurdle, it was smooth sailing. Even if Haskell seems strange to you at first, dont give up. Learning Haskell is almost like learning to program for the first time all over again. Its fun, and it forces you to think differently. NOTE If you ever get really stuck, the IRC channel #haskell on the freenode network is a great place to ask questions. The people there tend to be nice, patient, and understanding. Theyre a great resource for Haskell newbies. So, Whats Haskell? Haskell is a purely functional programming language. In imperative programming languages, you give the computer a sequence of tasks, which it then executes. While executing them, the computer can change state. For instance, you can set the variable a to 5 and then do some stuff that might change the value of a. There are also flow-control structures for executing instructions several times, such as for and while loops. Purely functional programming is different. You dont tell the computer what to doyou tell it what stuff is. For instance, you can tell the computer that the factorial of a number is the product of every integer from 1 to that number or that the sum of a list of numbers is the first number plus the sum of the remaining numbers. You can express both of these operations as functions. > Read the Introduction (PDF) in its entirety.

Miran Lipovaca: author's other books


Who wrote Learn You a Haskell for Great Good!: A Guide for Beginners? Find out the surname, the name of the author of the book and a list of all author's works by series.

Learn You a Haskell for Great Good!: A Guide for Beginners — 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 "Learn You a Haskell for Great Good!: A Guide for Beginners" 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
A Fistful of Monads

When we first talked about functors, we saw that they were a useful concept for values that can be mapped over. Then, we took that concept one step further by introducing applicative functors, which allow us to view values of certain data types as values with contexts and use normal functions on those values while preserving the meaning of those contexts.

In this chapter, we'll learn about monads, which are just beefed up applicative functors, much like applicative functors are only beefed up functors.

When we started off with functors we saw that its possible to map functions - photo 1

When we started off with functors, we saw that it's possible to map functions over various data types. We saw that for this purpose, the Functor type class was introduced and it had us asking the question: when we have a function of type a -> b and some data type f a , how do we map that function over the data type to end up with f b ? We saw how to map something over a Maybe a , a list [a] , an IO a etc. We even saw how to map a function a -> b over other functions of type r -> a to getfunctions of type r -> b . To answer this question of how to map a function over some data type, all we had to do was look at the typeof fmap :

  1. fmap :: ( Functor f) => (a -> b) -> f a -> f b
fmap :: (Functor f) => (a -> b) -> f a -> f b

And then make it work for our data type by writing the appropriate Functor instance.

Then we saw a possible improvement of functors and said, hey, what if that function a -> b is already wrapped inside a functor value? Like, what if we have Just (*3) , how do we apply that to Just 5 ? What if we don't want to apply it to Just 5 but to a Nothing instead? Or if we have [(*2),(+4)] ,how would we apply that to [1,2,3] ? How would that work even? For this, the Applicative type class was introduced, in which we wanted the answer to the following type:

  1. (<*>) :: ( Applicative f) => f (a -> b) -> f a -> f b
(<*>) :: (Applicative f) => f (a -> b) -> f a -> f b

We also saw that we can take a normal value and wrap it inside a data type. For instance, we can take a and wrap it so that it becomes a Just 1 . Or we can make it into a [1] . Or an I/O action that does nothing and just yields. The function that does this is called pure .

Like we said, an applicative value can be seen as a value with an added context. A fancy value, to put it in technical terms. For instance, the character 'a' is just a normal character, whereas Just 'a' has some added context. Instead of a Char , we have a Maybe Char ,which tells us that its value might be a character, but it could also be an absence of a character.

It was neat to see how the Applicative type class allowed us to use normal functions on these values with context and how that context was preserved. Observe:

  1. ghci> (*) <$> Just <*> Just
  2. Just
  3. ghci> (++) <$> Just "klingon" <*> Nothing
  4. Nothing
  5. ghci> (-) <$> [ , ] <*> [ , , ]
  6. [ , , , , , ]
ghci> (*) <$> Just 2 <*> Just 8Just 16ghci> (++) <$> Just "klingon" <*> NothingNothingghci> (-) <$> [3,4] <*> [1,2,3][2,1,0,3,2,1]

Ah, cool, so now that we treat them as applicative values, Maybe a values represent computations that might have failed, [a] values represent computations that have several results (non-deterministic computations), IO a values represent values that have side-effects, etc.

Monads are a natural extension of applicative functors and with them we're concerned with this: if you have a value with a context, m a , how do you apply to it a function that takes a normal a and returns a value with a context? That is, how do you apply a function of type a -> m b to a value of type m a ? So essentially, we will want this function:

  1. (>>=) :: ( Monad m) => m a -> (a -> m b) -> m b
(>>=) :: (Monad m) => m a -> (a -> m b) -> m b

If we have a fancy value and a function that takes a normal value but returns a fancy value, how do we feed that fancy value into the function? This is the main question that we will concern ourselves when dealing with monads. We write m a instead of f a because the m stands for Monad , but monads are just applicative functors that support >>= . The >>= function is pronounced as bind.

When we have a normal value a and a normal function a -> b it's really easy to feed the value to the function you just apply the function to the value normally and that's it. But when we're dealing with values that come with certain contexts, it takes a bit of thinking to see how these fancy values are fed to functions and how to take into account their behavior, but you'll see that it's easy as one two three.

Getting our feet wet with Maybe
Now that we have a vague idea of what monads are about lets see if we can - photo 2

Now that we have a vague idea of what monads are about, let's see if we can make that idea a bit less vague.

Much to no one's surprise, Maybe is a monad, so let's explore it a bit more and see if we can combine it with what we know about monads.

Make sure you understand at this point. It's good if you have a feel for how the various Applicative instances work and what kind of computations they represent, because monads are nothing more than taking our existing applicative knowledge and upgrading it.

A value of type Maybe a represents a value of type a with the context of possible failure attached. A value of Just "dharma" means that the string "dharma" is there whereas a value of Nothing represents its absence, or if you look at the string as the result of a computation, it means that the computation has failed.

When we looked at Maybe as a functor, we saw that if we want to fmap a function over it, it gets mapped over the insides if it's a Just value, otherwise the Nothing is kept because there's nothing to map it over!

Like this:

  1. ghci> fmap (++ "!" ) ( Just "wisdom" )
  2. Just "wisdom!"
  3. ghci> fmap (++ "!" ) Nothing
  4. Nothing
ghci> fmap (++"!") (Just "wisdom")Just "wisdom!"ghci> fmap (++"!") NothingNothing

As an applicative functor, it functions similarly. However, applicatives also have the function wrapped. Maybe is an applicative functor in such a way that when we use <*> to apply a function inside a Maybe to a value that's inside a Maybe , they both have to be Just values for the result to be a Just value, otherwise the result is Nothing . It makes sense because if you're missing either the function or the thing you're applying it to, you can't make something up out of thin air, so you have to propagate the failure:

  1. ghci> Just (+ ) <*> Just
  2. Just
  3. ghci> Nothing <*> Just "greed"
  4. Nothing
  5. ghci> Just ord <*> Nothing
  6. Nothing
ghci> Just (+3) <*> Just 3Just 6ghci> Nothing <*> Just "greed"Nothingghci> Just ord <*> NothingNothing

When we use the applicative style to have normal functions act on Maybe values, it's similar. All the values have to be Just values, otherwise it's all for Nothing !

  1. ghci> max <$> Just <*> Just
  2. Just
  3. ghci> max <$> Just <*> Nothing
  4. Nothing
ghci> max <$> Just 3 <*> Just 6Just 6ghci> max <$> Just 3 <*> NothingNothing

And now, let's think about how we would do >>= for Maybe . Like we said, >>= takes a monadic value, and a function that takes a normal value and returns a monadic value and manages to apply that function to the monadic value. How does it do that, if the function takes a normal value? Well, to do that, it has to take into account the context of that monadic value.

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Learn You a Haskell for Great Good!: A Guide for Beginners»

Look at similar books to Learn You a Haskell for Great Good!: A Guide for Beginners. 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 «Learn You a Haskell for Great Good!: A Guide for Beginners»

Discussion, reviews of the book Learn You a Haskell for Great Good!: A Guide for Beginners 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.