Accessing the return values of a complex variable in prolog

Viewed 41

i am new in prolog and I trying to achieve the following:

I have these 2 rules so far.

family(
    person(tom, fox, date(7, may, 1950), works(bbc, 15200)),
    person(ann, fox, date(9, may, 1951), unemployed),
     [person(pat, fox, date(5, may, 1973), unemployed),
    person(jim, fox, date(5, may, 1973), unemployed)]
    ).

child(X):- 
    family(_, _, Children),member(X, Children).

When I call the child(X) I get:

X = person(pat, fox, date(5, may, 1973), unemployed) ;
X = person(jim, fox, date(5, may, 1973), unemployed)

and what I seek to do is find whether a child is unemployed. But since X is returning the person() and not a single value, how can I access only the last element of this rule and check if its value is equal to "unemployed"? So basically I want to create a new rule looking something like this:

childworking(X):-
  child(X), (check if the last value of the person() is equal to unemployed).

If the value of child(X) was returned in a list I could do something like child(X),member(unemployed,X) but this is not the case... Thank you!

2 Answers

One way:

childworking(X) :-
    child(X), X = person(_,_,_,unemployed)

Says that X must be a child, and X must be a person of any firstname, any surname, any birthdate and unemployed job status.

Another way: the predicate =.. "univ" can change things between compound and list forms, then you can use list predicates to get the last thing, e.g.

?- person(pat, fox, date(5, may, 1973), unemployed) =.. List

List = [person, pat, fox, date(5, may, 1973), unemployed]

So:

childworking(X) :-
    child(X), X=..Y, last(Y, unemployed).
Related