Lesson 42. Efficient, stateful arrays in Haskell

 

After reading lesson 42, you’ll be able to

  • Use the UArray type for efficient storage and retrieval
  • Perform stateful computation on arrays with STUArray
  • Treat properly encapsulated, stateful functions as pure functions

After finishing this book on Haskell, you get a call from a recruiter at GooMicroBook asking if you’d like to interview. You say you’d love to, and the recruiter mentions that there will be a coding interview in the programming language of your choice. “Any language I want?” you eagerly ask. The recruiter confirms that, yes, you can use any language. With delight, you say you’d like to do the interview in Haskell.

You get to your interview, and your interviewer walks in the room. She asks you to solve some algorithm questions on the whiteboard. After eating, breathing, and dreaming about Haskell for many months, you can hardly wait to show of your elite programming skills. She starts off with a common question: “Implement a linked list for me.” You run up to the board and write this:

data MyList a = EmptyList | Cons a (MyList a)

42.1. Creating efficient arrays in Haskell with the UArray type

42.2. Mutating state with STUArray

42.3. Taking values out of the ST context

42.4. Implementing a bubble sort

Summary