3 ms·
The article plays with the idea of introducing "uncons" to the language, which corresponds to a Haskell expression (simplified; also possible in other ML langua
by lobster_johnson 12y ago
The article plays with the idea of introducing "uncons" to the language, which corresponds to a Haskell expression (simplified; also possible in other ML languages as well as Erlang) such as this:
x:xs = someList
This means: Assign the head (ie. the first element) of someList to the variable x, and the remaining elements to the variable xs.
In Haskell, ": "is the list concatenation operator. (So if you think about it, the left hand side and the right hand side are in fact together an equation.) The ":" operator is often called "cons", and it's really a function which takes a head and a tail and returns a new list; hence, "uncons" is the reverse, breaking apart a list into an element and a new list.
This is useful because when processing sequences of stuff, you often want to look at the first element, do something, and the process the rest. A linked list is per definition a head and a tail, whereas other structures such as arrays and even files can be adapted into this abstraction. Even a virtual sequence such as a random generator can adopt this protocol: The head of the random generator is the next random number; the tail is the new state of the generator.
The article then tries to formulate this uncons operation as a Swift protocol, and talks about ways to leverage it for different types and functional constructs. Those constructs (such as fold) are the basic tools of functional programming, but will admittedly be completely unfamiliar to someone who hasn't touched the topic before.
- tel 12y agoWell said!