Languages & Environments



Download 2.16 Mb.
Page6/10
Date28.05.2018
Size2.16 Mb.
#51457
1   2   3   4   5   6   7   8   9   10

Recursion


Another key feature of declarative languages is the use of recursion. Rules are normally created to call other facts or rules. Recursion takes place when a rule calls itself. A commonly used example of recursion is that of a rule about ancestry.

ancestor_of(X,Y):- parent_of(X,Z), ancestor_of(Z,Y)

This reads as 'X is an ancestor of Y if X is a parent of Z and Z is an ancestor of Y'. Recursion is evident here as the rule ancestor_of(X,Y) calls itself within the rule definition – ancestor_of(Z,Y).

Activity: PROLOG


This PROLOG program consists of a set of facts about a set of individuals. The

facts describe who is warm, who is fed and who is cold. There are two simple rules to



determine whether someone is happy or unhappy.

Match the answers to the following queries:



1) ?warm(fred)

a) Yes


b) No

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



2) ?happy(jack)

a) Yes


b) No

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



3) ?happy(fred)

a) Yes


b) No

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



4) ?unhappy(X)

a) X = jim

b) X = fred

c) X = justin

d) X = jim, X = Justin

Object-orientated languages

Procedural languages rely on the process of passing data from one procedure to another. Procedures and the code within them exist independently of the data. They simply access data deemed relevant by the programmer as and when necessary. The data used is separate from the code.

However, object-oriented languages treat data and code as part of the same object. Firstly a class is created. A class acts as a template for an object and classes are examined in more detail later in this section. Objects are created within a class and will contain code and the data that the code requires. Data and code is not held separately. The name given to the concept of an object containing both the code and its related data is called 'encapsulation'.



In our world there are many objects such as cars, boats, buses, doors etc. To think of objects in this way is a good starting point. An object such as a train will have a set of attributes.




Download 2.16 Mb.

Share with your friends:
1   2   3   4   5   6   7   8   9   10




The database is protected by copyright ©ininet.org 2024
send message

    Main page