I saw this at a function ([]:_)
Cannot find a definition of it (Google doesn't work well with symbols). So what is it anyway?
type Mat a = [[a]]
myTranspose :: Mat a -> Mat a
myTranspose ([]:_) = []
myTranspose p = (map head p) : myTranspose (map tail p)
It is pattern matching for a list of lists. It matches with a list with at least one element where the first element is the empty list. For instance [[]], or [[], [2,4]], [[], [], [1,4], [2], [5]].
A list in Haskell is defined as a linked list with two constructors: [], the empty list, and (a:as) the "cons" where a is the "head" (the first element of the list), and as the tail (a list that contains the remaining elements).
Furthermore the underscore _ is used as a "don't care" variable. So it means that we look for a cons pattern (a:as), where a (the first element) is an empty list [], and as is _ so we are not interested in the remaining elements of the list.
In the case of the myTranspose function, it will thus result in an empty list if you give it a list of lists with the first item being an empty list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With