Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Soar with Haskell

You're reading from  Soar with Haskell

Product type Book
Published in Dec 2023
Publisher Packt
ISBN-13 9781805128458
Pages 418 pages
Edition 1st Edition
Languages
Author (1):
Tom Schrijvers Tom Schrijvers
Profile icon Tom Schrijvers

Table of Contents (23) Chapters

Preface 1. Part 1:Basic Functional Programming
2. Chapter 1: Functions 3. Chapter 2: Algebraic Datatypes 4. Chapter 3: Recursion 5. Chapter 4: Higher-Order Functions 6. Part 2: Haskell-Specific Features
7. Chapter 5: First-Class Functions 8. Chapter 6: Type Classes 9. Chapter 7: Lazy Evaluation 10. Chapter 8: Input/Output 11. Part 3: Functional Design Patterns
12. Chapter 9: Monoids and Foldables 13. Chapter 10: Functors, Applicative Functors, and Traversables 14. Chapter 11: Monads 15. Chapter 12: Monad Transformers 16. Part 4: Practical Programming
17. Chapter 13: Domain-Specific Languages 18. Chapter 14: Parser Combinators 19. Chapter 15: Lenses 20. Chapter 16: Property-Based Testing 21. Index 22. Other Books You May Enjoy

Traversables

Recall that the functorial map (fmap) lifts pure functions of the a -> b type to functions on collections of the t a -> t b type. The Traversable type class generalizes this to effectful functions, a -> f b, with f as an applicative functor.

First, let’s compare the two for lists (viewed as a collection, not as an effect):

fmap :: (a -> b) -> ([a] -> [b])
fmap f []     = []
fmap f (x:xs) = f x : fmap f xs
traverse :: Applicative f => (a -> f b) -> ([a] -> f [b])
traverse f []     = pure []
traverse f (x:xs) = pure (:) <*> f x <*> traverse f xs

As we can see, the structure is quite similar. The main difference is that traverse uses the application, (<*>), of applicative functors, and lifts the list constructors with pure.

The Traversable class

The type class declaration looks as follows:

Prelude
class (Functor t, Foldable t) => Traversable t where...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}