Home > database >  Haskell use of getters instead of pattern match
Haskell use of getters instead of pattern match

Time:09-28

I have a simple nested data structure I can pattern match:

Prelude> data Token = Token String Int deriving ( Show )
Prelude> data Person = Person Token deriving ( Show )
Prelude> person = Person (Token "moish" 74)
Prelude> foo (Person (Token name _)) = "hello "    name

Is there an easy way to extract the name (moish) from p without pattern-match ?

foo :: Person -> String
foo p = -- ???

CodePudding user response:

Nope. Pattern match is the one and only way to consume data. Everything else is built on top of that.

  • Related