15 Oct 2017, 03:30
Of course you can always create a function to perform the task of dividing the head from the tail in a list, but the deep meaning of formalism [H|T] is another.
[H|T] is a formalism that allows you to assign a list item to a variable without using the functions, it is as if it were a mask that captures the elements through their position.
Let me explain better with examples.
If we have a list of Objects = [A, B, C, D, E(e1), F]
we can filter the contents of the list with the following formalisms [H|T] and assign the respective variables:
[Head|Tail] → Head = A, Tail = [B, C, D, E(e1), F]
[Head, Head2|Tail] → Head = A, Head2 = B, Tail = [C, D, E(e1), F]
[Head, Head2, Head3, Head4|Tail] → Head = A, Head2 = B, Head3 = C, Head4 = D, Tail = [E(e1), F]
[Head, Head2, Head3, Head4, Head5, Head6|Tail] → Head = A, Head2 = B, Head3 = C, Head4 = D, Head5 = E(e1), Head6 = F, Tail = []
Being [H|T] a formalism, it helps to write a more compact and readable code, using a native way to divide the elements of a list.