July 2023
Intermediate to advanced
670 pages
17h 13m
English
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?
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 ...