How can I access the members of a custom made class if they are not named?

  • A+
Category:Languages

I've got a predefined class:

data Wave = Wave [Double] deriving (Show, Eq) 

I want to use the function 'map' with the class, and so I have to access the [Double]-value. But I do not know how.

I looked here: Accessing members of a custom data type in Haskell, but that didn't answer my question, as I can't change (or name parameters of) the class.

I tried to write:

waveToDoubleList :: Wave -> [Double]     (Wave wave) = wave 

 


You forgot to include the function name:

waveToDoubleList :: Wave -> [Double] waveToDoubleList (Wave wave) = wave ---------------- 

Now it should work.

(Wave wave) is known as a pattern. This function works by pattern matching.

Wave is used in two roles: as a data constructor, or pattern name; and as the type's name.

When starting learning Haskell, you might want to use separate names for these roles:

data Wave = MkWave [Double] deriving (Show, Eq)  waveToDoubleList :: Wave -> [Double] waveToDoubleList (MkWave wave) = wave 

Here Wave is a type; we create values of this type with MkWave data constructor.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: