What is a ‘nested’ pattern in Haskell. I hear the term everywhere but am not sure what the it actually means. How would you define it? Any examples?
Thanks in advance.
EDITED TO ADD: (as quoted in textbook on request)
“Patterns can contain literals and nested patterns, as in the examples:
addPair (0,y) = y
addPair (x,y) = x+y
shift :: ((Int,Int),Int) -> (Int,(Int,Int))
shift ((x,y),z) = (x,(y,z))
This means that you can match against a pattern that contains another pattern. In your example, the
(x, y)pattern is contained inside the larger((x, y), z)pattern. The nesting can be arbitrarily deep, e.g. all of the following are legal:and so on. This also extends to lists and algebraic datatypes:
Here,
ftakes a list of lists,f'takes a list of lists of lists,gtakes aMaybethat contains anotherMaybe(that is,Maybe (Maybe a)), andg'takes aMaybe (Maybe (Maybe a))