Skip to Content
Effective Haskell
book

Effective Haskell

by Rebecca Skinner
July 2023
Intermediate to advanced
670 pages
17h 13m
English
Pragmatic Bookshelf
Content preview from Effective Haskell

Exercises

Flipping the Script

Try to write instances of Functor, Applicative, and Monad for List where Functor is defined in terms of Applicative, and Applicative is defined in terms of Monad. Is this possible? Why or why not?

Not a Functor

Imagine that we’ve created a new type to represent a sorted list of values:

 {-# LANGUAGE DerivingStrategies #-}
 module​ ​SortedListFunctor​ (​SortedList​, ​insertSorted​) ​where
 
 data​ ​SortedList​ a = ​Empty​ | ​Cons​ a (​SortedList​ a)
 deriving​ stock (​Eq​, ​Show​)
 
 insertSorted :: ​Ord​ a => a -> ​SortedList​ a -> ​SortedList​ a
 insertSorted a ​Empty​ = ​Cons​ a ​Empty
 insertSorted a (​Cons​ b bs)
  | a >= b = ​Cons​ b (insertSorted a bs)
  | otherwise = ​Cons​ a (​Cons​ b bs)

Although ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Grokking Functional Programming

Grokking Functional Programming

Michal Plachta
Grokking Algorithms

Grokking Algorithms

Aditya Bhargava
The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

Steve Klabnik, Carol Nichols

Publisher Resources

ISBN: 9798888650400Errata Page