Up Next

4.1  Structure Notation

In ECLiPSe, structure fields can be given names. This makes it possible to write structures in a more readable and maintainable way. Such structures first need to be declared by specifying a template like:

:- local struct( book(author, title, year, publisher) ).
Structures with the functor book/4 can then be written as

book{}
book{title:'tom sawyer'}
book{title:'tom sawyer', year:1876, author:twain}
which, in canonical syntax, correspond to the following:

book(_, _, _, _)
book(_, 'tom sawyer', _, _)
book(twain, 'tom sawyer', 1876, _)
There is absolutely no semantic difference between the two syntactical forms. The special struct-syntax with names has the advantage that Sometimes it is necessary to refer to the numerical position of a structure field within the structure, e.g. in the arg/3 predicate:

arg(3, B, Y)
When the structure has been declared as above, we can write instead:

arg(year of book, B, Y)
Declared structures help readability, and make programs easier to modify. In order not to lose these benefits, one should always use curly-bracket and of-syntax when working with them, and never write them in canonical syntax or referring to argument positions numerically.
See also the update_struct/4 built-in predicate.

Up Next