Pointers are created using the ampersand character (&). An ampersand without arguments represents the identity pointer. The tilde character (~) is used to invoke pointer expressions, it evaluates a pointer to the function induced by it. To understand what this means, the different types of pointer expressions must be discussed.
Deconstructors are one type of pointer expression, they are used to deconstruct a data structure.
They take a data structure as an argument and return a component of the argument as a result. Functions such as
tail and head in functional languages can be thought of as deconstructors. Ursala offers a rich set of deconstructors for manipulating various data types.To illustrate the idea of pointers more clearly, let us take an example of manipulating a list. Lists in Ursala are comma separated values(numbers, strings, ...) delimited by angle brackets. To ease in reading, any line of code preceded by 4 spaces is an expression entered at the prompt, the result is displayed below the input with no spaces preceding it. Comments are prefixed by a #.
<1,2,3,4>
<1,2,3,4>
# identity pointer
&
&
# call the identity pointer
~&
<0,0>
~& <1,2,3>
<1,2,3>
~&h <1,2,3>
1
~&t <1,2,3>
<2,3>
Looking at the examples above, it is clear that h and t represent the head and tail deconstructors respectively. Deconstructors may be chained together to form more complex deconstructors.
~&tt <1,2,3,4>
<3,4>
~&th <1,2,3,4>
2
~&ttt <1,2,3,4>
<4>
Since the h and t deconstructors operate on lists, such an expression is invalid.
~&ht <1,2,3,4>
0 # zero represents false or nil in Ursala.
It is now clear how we can extract single elements from a data structure, but what if we want to extract more elements? We may achieve this by arranging the pointer expressions in the form we wish to receive the final output. For a deconstructor returning a tuple data structure, we arrange the pointer expressions in the form of a tuple. For a deconstructor returning a list, Ursala provides a semicolon as syntactic sugar (think of it as a cons operator) which joins a head(single element) on the left of the semicolon to the tail(a list).
~(&ll, &rr) ((1,2),(3,4))
(1,4)
~&h:&tt <1,2,3,4>
<1,3,4>
The need for a less clumsy syntax when manipulating complex compound deconstructors is the reason constructors exist, they are the second type of pointer expression in Ursala. A constructor is a combinator which takes deconstructors as arguments, manipulating them in such a way as to construct a data structure from their results.
The X constructor takes two consecutive deconstructors on its left hand side and groups them to form a pair.
~&rlX (1,2)
(2,1)
There are constructors for each data structure, the C constructor (or cons) is used to join the result of two pointer expressions as a list. Constructors allow us to chain more deconstructors together, forming more complex selections.
~&htC <1,2,3,4>
<1,2,3,4>
~&hrlX <(1,2),(3,4)>
(1,1)
A table of the various data structures and their corresponding deconstructors/constructors is available in the Ursala manual.

Later I will be discussing more of the available constructors and deconstructors.
(Continued later)
No comments:
Post a Comment