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

Writing Type Classes Representing Emptiness

Imagine that we wanted to create a type class that represents things that can be “empty” for some definition of empty that will depend on the particular type. In this exercise, we’ll call the type class Nullable and give it two functions:

  • isNull should return True if a value is “empty”.
  • null should return an “empty” value.
 module​ ​Nullable​ ​where
 import​ ​Prelude​ ​hiding​ (​null​)
 
 class​ ​Nullable​ a ​where
  isNull :: a -> ​Bool
  null :: a

Create instances of this type class for:

  1. Any Maybe a where a is Nullable
  2. Any tuple, (a,b) where a and b are Nullable
  3. Any list type

Adding a Default Null Test

Add a new Eq constraint to the definition of Nullable:

 class​ ​Eq​ a => ​Nullable
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