Home > OS >  Printing a custom representation of my own datatype in Haskell
Printing a custom representation of my own datatype in Haskell

Time:09-30

Context

Important detail, I am using Haskell (NOT CABAL) on repl.it. I would like to print a custom Haskell datatype that I created. I was thinking of an approach similar to Python's __repr__ or __str__ methods, when creating a new class. Something like:

class Length:
    def __init__(self, value, unit_of_measurement):
        self.value = value
        self.unit_of_measurement = unit_of_measurement
    def __str__(self):
        return f'{self.value}{self.unit_of_measurement}'
    def __repr__(self):
        return self.__str__()

Which will produce the following:

>>> # I can print a custom representation of the object
>>> l = Length(10, 'cm')
>>> l
10cm
>>> print(l)
10cm

The problem

I am trying to instantiate the Show class in my custom datatype and use pattern matching to customize the output that will be sent to the console.

What I tried so far

-- This works fine
data Length = Length {value :: Double, unit_of_measurement :: String}
    deriving (Eq, Ord)  -- I don't want to use default Show inheritance

-- These lines are actually wrong, but I don't know how to solve this
-- Also, how to get the fields declared in my datatype???
instance Show Length where  -- Or IO, I am not sure
    print Length = print $ show value    unit_of_measurement

Ignoring the wrong lines that I mentioned (so the compiler won't stop the execution) and considering I have used Haskell's built-in inheritance mechanism with Show (deriving(Show)) this will be the result, (which I don't like):

λ> :load Main.hs
[1 of 1] Compiling Main             ( Main.hs, interpreted )
Ok, one module loaded.
λ> let test = Length 10 "cm"
λ> test
Length {value = 10.0, unit_of_measurement = "cm"}  -- HERE            
  • Related