Frage

I am trying to understand abstract data types. In the code below I am supposed to write a function for an abstract data type. createMatrix takes in a tuple of the number of x rows and y columns and some element z and fills in the matrix with that element.

I am new to Haskell and am okay with all the basics, but am extremely confused on how to implement this function for an abstract data type.

I would greatly appreciate any suggestions on how to do this.

Many thanks in advance!

newType Matrix a = Mat ((Int, Int), (Int,Int) -> a)


createMatrix (x,y) z = 
War es hilfreich?

Lösung

You have got as far as

createMatrix (x,y) z = {-TODO-}

The obvious next step is

createMatrix (x,y) z = Mat ( {-TODO-} , {-TODO-} )

Why should this be obvious?

  • Your function gives back a value of type Matrix a; this has only one constructor, Mat: so that is what that value must start with.
  • The Mat constructor take a single value, a pair. We don't have a pair of the correct type to hand, so we will have to make one. Again, there is only a single constructor, ( , ), so that's what we use.

Importantly, I have done this just by following the types (and assuming you don't have any helper functions ready to call).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top