Using PROLOG 1) Write a recursive predicate: maledescend(FATHER,DESCEND) that lists all male descendants of the given FA
Posted: Mon Jun 06, 2022 6:27 pm
Using PROLOG
1) Write a recursive predicate: maledescend(FATHER,DESCEND) that
lists all male descendants of the given FATHER.
2) Write a recursive predicate:sons(CHILDREN,SONS) that takes a
list of CHILDREN and produces a list of SONS.
3) Write a predicate:children(FATHER,CHILDREN) that produces a
list of a FATHER’s CHILDREN.
4) Write the recursive function:findeldest(PEOPLE,ELDEST) that
takes a list of PEOPLE and returns the ELDEST person (i.e. the
person born first).
Thus passing a list of sons would produce the eldest son.
Data:
% Facts
2 % wellesley(NAME,GENDER,dates(BORN,DIED)
3 %
4 wellesley(arthur,m,dates(1769,1852)). % First Duke of
Wellington
5 wellesley(catherine,f,dates(1773,1831)).
6 wellesley(arthur,m,dates(1807,1884)). % Second Duke of
Wellington
7 wellesley(charles,m,dates(1808,1858)).
8 wellesley(augusta,f,dates(unk,1893)).
9 wellesley(georgina,f,dates(unk,1880)).
10 wellesley(mary,f,dates(unk,1936)).
11 wellesley(henry,m,dates(1846,1900)). % Third Duke of
Wellington
12 wellesley(victoria,f,dates(1848,1933)).
13 wellesley(arthur,m,dates(1849,1934)). % Fourth Duke of
Wellington
14 wellesley(kathleen,f,dates(1849,1927)).
15 wellesley(arthur,m,dates(1876,1941)). % Fifth Duke of
Wellington
16 %
17 % Marriage Relationships
18 %
19 married(wellesley(arthur,m,dates(1769,1852)),
wellesley(catherine,f,dates(1773,1831))).
20 married(wellesley(charles,m,dates(1808,1858)),
wellesley(augusta,f,dates(unk,1893))).
21 married(wellesley(arthur,m,dates(1849,1934)),
wellesley(kathleen,f,dates(1849,1927))).
22 %
23 % Father -> Child Relationships
24 %
25 father(wellesley(arthur,m,dates(1769,1852)),
wellesley(arthur,m,dates(1807,1884))).
26 father(wellesley(arthur,m,dates(1769,1852)),
wellesley(charles,m,dates(1808,1858))).
27 father(wellesley(charles,m,dates(1808,1858)),
wellesley(georgina,f,dates(unk,1880))).
28 father(wellesley(charles,m,dates(1808,1858)),
wellesley(mary,f,dates(unk,1936))).
29 father(wellesley(charles,m,dates(1808,1858)),
wellesley(henry,m,dates(1846,1900))).
30 father(wellesley(charles,m,dates(1808,1858)),
wellesley(victoria,f,dates(1848,1933))).
31 father(wellesley(charles,m,dates(1808,1858)),
wellesley(arthur,m,dates(1849,1934))).
32 father(wellesley(arthur,m,dates(1849,1934)),
wellesley(arthur,m,dates(1876,1941))).
33 %
34 % To find the next Duke, by male primogeniture:
35 % - Eldest son of the Duke (if alive)
36 % - Duke's Brother (if alive; in this case, each Duke has only
one brother, otherwise eldest brother)
37 % - Eldest son of Duke's brother (if alive)
38 %
39
nextduke(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D)))
:-
40
eldestson(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D))),
41 B < DD,
42 D > DD, !.
43
nextduke(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D)))
:-
44
brother(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D))),
45 B < DD,
46 D > DD, !.
47
nextduke(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D)))
:-
48 brother(wellesley(DNAME,DG,dates(DB,DD)),BROTHER),
49 eldestson(BROTHER,wellesley(NAME,G,dates(B,D))),
50 B < DD,
51 D > DD, !.
52
53 eldestson(DUKE,HEIR) :-
54 children(DUKE, CHILDREN), sons(CHILDREN,SONS),
findeldest(SONS,HEIR).
1) Write a recursive predicate: maledescend(FATHER,DESCEND) that
lists all male descendants of the given FATHER.
2) Write a recursive predicate:sons(CHILDREN,SONS) that takes a
list of CHILDREN and produces a list of SONS.
3) Write a predicate:children(FATHER,CHILDREN) that produces a
list of a FATHER’s CHILDREN.
4) Write the recursive function:findeldest(PEOPLE,ELDEST) that
takes a list of PEOPLE and returns the ELDEST person (i.e. the
person born first).
Thus passing a list of sons would produce the eldest son.
Data:
% Facts
2 % wellesley(NAME,GENDER,dates(BORN,DIED)
3 %
4 wellesley(arthur,m,dates(1769,1852)). % First Duke of
Wellington
5 wellesley(catherine,f,dates(1773,1831)).
6 wellesley(arthur,m,dates(1807,1884)). % Second Duke of
Wellington
7 wellesley(charles,m,dates(1808,1858)).
8 wellesley(augusta,f,dates(unk,1893)).
9 wellesley(georgina,f,dates(unk,1880)).
10 wellesley(mary,f,dates(unk,1936)).
11 wellesley(henry,m,dates(1846,1900)). % Third Duke of
Wellington
12 wellesley(victoria,f,dates(1848,1933)).
13 wellesley(arthur,m,dates(1849,1934)). % Fourth Duke of
Wellington
14 wellesley(kathleen,f,dates(1849,1927)).
15 wellesley(arthur,m,dates(1876,1941)). % Fifth Duke of
Wellington
16 %
17 % Marriage Relationships
18 %
19 married(wellesley(arthur,m,dates(1769,1852)),
wellesley(catherine,f,dates(1773,1831))).
20 married(wellesley(charles,m,dates(1808,1858)),
wellesley(augusta,f,dates(unk,1893))).
21 married(wellesley(arthur,m,dates(1849,1934)),
wellesley(kathleen,f,dates(1849,1927))).
22 %
23 % Father -> Child Relationships
24 %
25 father(wellesley(arthur,m,dates(1769,1852)),
wellesley(arthur,m,dates(1807,1884))).
26 father(wellesley(arthur,m,dates(1769,1852)),
wellesley(charles,m,dates(1808,1858))).
27 father(wellesley(charles,m,dates(1808,1858)),
wellesley(georgina,f,dates(unk,1880))).
28 father(wellesley(charles,m,dates(1808,1858)),
wellesley(mary,f,dates(unk,1936))).
29 father(wellesley(charles,m,dates(1808,1858)),
wellesley(henry,m,dates(1846,1900))).
30 father(wellesley(charles,m,dates(1808,1858)),
wellesley(victoria,f,dates(1848,1933))).
31 father(wellesley(charles,m,dates(1808,1858)),
wellesley(arthur,m,dates(1849,1934))).
32 father(wellesley(arthur,m,dates(1849,1934)),
wellesley(arthur,m,dates(1876,1941))).
33 %
34 % To find the next Duke, by male primogeniture:
35 % - Eldest son of the Duke (if alive)
36 % - Duke's Brother (if alive; in this case, each Duke has only
one brother, otherwise eldest brother)
37 % - Eldest son of Duke's brother (if alive)
38 %
39
nextduke(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D)))
:-
40
eldestson(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D))),
41 B < DD,
42 D > DD, !.
43
nextduke(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D)))
:-
44
brother(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D))),
45 B < DD,
46 D > DD, !.
47
nextduke(wellesley(DNAME,DG,dates(DB,DD)),wellesley(NAME,G,dates(B,D)))
:-
48 brother(wellesley(DNAME,DG,dates(DB,DD)),BROTHER),
49 eldestson(BROTHER,wellesley(NAME,G,dates(B,D))),
50 B < DD,
51 D > DD, !.
52
53 eldestson(DUKE,HEIR) :-
54 children(DUKE, CHILDREN), sons(CHILDREN,SONS),
findeldest(SONS,HEIR).