10 Digital music box
This chapter covers
- Correctly dealing with numeric values and their type classes
- Working with infinite lists
- Modeling a domain-specific problem using Haskell’s data types
- Building abstractions and high-level structures around low-level implementations
So far, we have been dealing with very earnest topics. Ancient encryption schemes, shortest paths, and CSV file mangling are all serious business. In this chapter, we want to be a bit more playful and let our creative spirit run free by making music! Don’t worry if you have no instrument ready at hand or lack any general idea of how to compose. We will use the power of Haskell not just to serve as our own musical synthesizer but also to function as a composition tool allowing us to easily create the next musical masterpiece remembered by audiences for centuries.
Modeling real-world data inside programs is a normal activity for any programmer. This chapter will specifically teach you how to think about abstracting details and modeling a part of a system in reality (the system of western music theory) as types and values. This kind of modeling comes up every time you want to write software that incorporates some kind of business logic or concepts not directly related to software specifications or architecture. Haskell allows for highly abstract modeling, which fully omits any details that are not relevant to the use case. This is what we are trying to achieve in this chapter.