Unit 6 - Trees, Graphs, Recursive Types Flashcards
In most cases, in custom types that allow values of arbitrary size, these values are some
kind of
trees or graphs
A tree is a
hierarchical structure with one root
A graph is a (in this context)
a collection of arbitrary links among a set of nodes.
In most PLs, graphs are more difficult to
represent and manipulate than trees
Imperative languages let us treat recursive structures as
nodes with explicit links between
them
Abstract languages present the recursive structures as
symbolic expressions.
Imperative PLs are characterised by
- more flexibility in organising the structures, in particular they support structures
with aliasing and cycles; - the danger of making hard-to-discover mistakes due to aliasing;
- the ability to modify and evolve structures that had been built earlier;
- the need to write much more code when creating and traversing trees;
- less PL support for checking the correctness of the code.
The main ways in which Java differs from C in treating recursive structures are:
- In Java there is no need to distinguish pointers to records (ie objects) from the re-
cords themselves because all Java records (ie objects) are on the heap and can be
referred to only by pointers. - There is no easy way to duplicate a whole record (ie object) in Java, whereas in C a
whole record can be copied by an ordinary assignment to a variable of the record
type. - In Java, new heap memory space is allocated and initialised with a correct record
value in a single statement (ie a constructor call), whereas in C, space of the right
size has to be first allocated by a special malloc call and then other statements
initialise the value stored in that space.
The main differences between Lisp and Haskell in treating symbolic expressions are:
- Constructors in Lisp symbolic expressions do not have to be declared and have
no interesting intrinsic types, whereas in Haskell it is the opposite: Each record
constructor symbol has to be explicitly declared and associated with a unique type. - Lisp trees can be modified using setf, but it has to be used with care to avoid
dangerous effects, such as modifying the program code. Haskell symbolic trees
cannot be modified, only re-created with some changes.