concept object in category haskell

appears as: objects, n object, object
Get Programming with Haskell

This is an excerpt from Manning's book Get Programming with Haskell.

Haskell doesn’t use objects, so why on earth should you spend time implementing OOP from scratch? The main reason is it allows you to understand the power of the functional tools you’ve been learning about so far. If you can understand how to build OOP by using closures, lambdas, and first-class functions, you’ve truly reached functional enlightenment.

Because you have first-class functions, you can treat this value stored in a closure just like data. You can now pass your stored information around like an object. But clearly this isn’t enough, as you can’t do anything interesting with the fact that you’ve stored your ounces. What you want is to be able to apply a message to that internal value of the cup. You’ll use a first-class function to pass a message to your object. This message can then act on the internal property of the object. Notice that you’re going to be using a slightly different pattern of sending messages to the object rather than the common approach of calling methods. When calling methods, your object > action pattern looks like figure 10.1.

Figure 10.1. Method-calling approach to OOP

Your approach will invert this pattern by sending a message to an object, as shown in figure 10.2.

You’ve stored your value in an object, but you need something useful for this object to do. Next you’ll create simple messages to get and set values inside your object. First you want to be able to get the volume of coffee currently in the cup. You’ll create a getOz message that takes a cup object and returns the number of fluid ounces (flOz) it has.

Listing 10.3. getOz message
getOz aCup = aCup (\flOz -> flOz)

To use this function, you pass this message into the object:

GHCi> getOz coffeeCup
12

Next you want to do something a little more complicated. The most useful thing to do with a cup is to drink from it! Drinking from a cup inherently changes the state of the object. But how in the world are you going to do this in Haskell!? Easy: you’ll create a new object behind the scenes. Your message to set a value for fluid ounces needs to return a new instance of your object with the internal property appropriately modified.

10.4. Types—objects and so much more!

Haskell isn’t an object-oriented language. All of the functionality built here from scratch already exists in a much more powerful form, using Haskell’s type system. Many of the ideas used in this section will come up again, but rather than hacking together objects, you’ll be creating types. Haskell’s types can replicate all the behavior you’ve modeled here, but give you the added benefit that Haskell’s compiler can reason much more deeply about types than your ad hoc objects. Because of this ability to reason about types, code created using a powerful type system tends to be much more robust and predictable. The advantages you’ve seen from using functional programming are magnified tremendously when you combine them with Haskell’s type system.