Why parameterized data type declaration in Haskell?

I watched a tube video and found out that a person has declared a new datatype using a parameterized datatype like:

data Quad a = Quad a a a a
instance  (Show a) => Show (Quad a) where
show (Quad a b c d) = (show a) ++ " " ++  -- code continues for printing

      

I doubt why we need the first line of code. I did something like this and it still works. My code:

data Q = Q Integer Integer Integer Integer 
instance  Show Q where
show (Q a b c d) = (show a)++" " ++ --code continues for printing

      

What is the use of the first line, namely data Quad a = Quad a a a a

when we can do it in my method as shown above? Please help. Thanks in advance!

+3


source to share


2 answers


I don't know how familiar you are with imperative programming languages ​​such as Java, but I assume that you are familiar with generics. Now your definition Q

would be more or less equivalent to something in Java, like:

public class Q {

    private Integer field1;
    private Integer field2;
    private Integer field3;
    private Integer field4;

    public Q (Integer field1, Integer field2, Integer field3, Integer field4) {
        this.field1 = field1;
        this.field2 = field2;
        this.field3 = field3;
        this.field4 = field4;
    }

}

      

(yes, I know it Integer

's not equivalent Integer

in Java in Haskell, but that's not a problem).

This now allows us to work with int

s. There may be a problem, what to do if we want to get a quaternion with double

s or Car

s. So it won't work very efficiently.

The original definition Quad

however would be something like Java:

public class Quad<A> {

    private A field1;
    private A field2;
    private A field3;
    private A field4;

    public Quad (A field1, A field2, A field3, A field4) {
        this.field1 = field1;
        this.field2 = field2;
        this.field3 = field3;
        this.field4 = field4;
    }

}
      



So you can work with Quad<Integer>

which is more or less equivalent to the above definition Q

. But I can easily work with Quad<Double>

and Quad<Car>

to build quaternions of doubles, cars, horses, ...

Now, you might be thinking that the horse quaternion makes no sense. Quaternions are usually specified numerically. But there are several data structures to represent numbers: it Integer

can - as the name suggests - only represent integral values. You might want to allow as well double

. Moreover, it Integer

can represent all integral values ​​(until memory is exhausted). While this is certainly a good feature, it comes with a cost: performance. It usually takes more time to complete the calculations by Integer

(even if they have the same value). Therefore, if you want to improve performance, you can search Quad Int16

for example.

By using a type parameter, you allow yourself flexibility: you don't need to define a data type that looks almost the same as another.

In addition, you can define - for example, Youtube tutorials tutorials - the instance types you put constraints on a

. So for everyone Quad a

where ed a

might be show

, you can show Quad a

. You can also set restrictions on functions. Let's say, for example, you want to provide a way to calculate the sum of the "items" of an ATV. You can define:

sumquad :: Num a => Quad a -> a
sumquad (Q x y z t) = x + y + z + t
      

So now you have defined a function sumquad

for all types Quad a

, where a

is an instance of the class Num

(and thus supports adding). If we work with Q

, for Q

, a sumquadQDouble

for QDouble

(hypothetical data QDouble = QDouble Double Double Double Double

) should be sumquadQ

, etc. Therefore, it will take a lot of work and result in less elegant code.

+6


source


Quad a a a a

allows you to create a quad containing any type, while Q Integer Integer Integer Integer

only allowing integers.

So it depends on what your quad should be able to store. If you're only storing integers, it should probably be called IntegerQuad. One reason might be that you want to implement a function like

--collapse :: Q -> Integer
collapse Q(a,b,c,d) = a+b+c+d

      



Which you can't do with if (+).

In Haskell, I believe you should exhibit as abstract, as this gives you the most flexibility and most likely you can reuse other functions.

+3


source







All Articles