1
votes

I want to get all planets and its moons O that are further away than planet P

% planets
orbits(mercury, sun).
orbits(venus, sun).
orbits(earth, sun).
orbits(mars, sun).
orbits(jupiter, sun).
orbits(saturn, sun).
orbits(uranus, sun).
orbits(neptune, sun).
ae(mercury, 0.39).
ae(venus, 0.72).
ae(earth, 1).
ae(mars, 1.52).
ae(jupiter, 5.20).
ae(saturn, 9.54).
ae(uranus, 19.22).
ae(neptune, 30.06).
% moons
orbits(moon, earth).
orbits(deimos, mars).
orbits(phobos, mars).
orbits(ganymede, jupiter).
orbits(callisto, jupiter).
orbits(io, jupiter).
orbits(europa, jupiter).
orbits(titan, saturn).
orbits(enceladus, saturn).
orbits(titania, uranus).
orbits(oberon, uranus).
orbits(umbriel, uranus).
orbits(ariel, uranus).
orbits(miranda, uranus).
orbits(triton, neptune).

First I tried to get all planets that are further away than P

outer_orbit(P,O):-ae(O,X),ae(P,Y),X>Y.

Now I need to print all Moons that are in O

How can I do that?

1

1 Answers

2
votes

Now I need to print all Moons that are in O

How can I do that?

It seems a work for findall/3

findall(X, orbits(X, P), Lm)

where P is the planet and Lm is the list of the moons.

The "print" part can be a simple

write(Lm)

But in this way you can find also the list of the planets, with

findall(X, orbits(X, sun), Lp)

If you want to find only moons, you can also impose that P orbits around the sun

findall(X, (orbits(P, sun), orbits(X, P)), Lm)

or a clause as follows

allMoons(P, Lm) :-
  orbits(P, sun),
  findall(X, orbits(X, P), Lm).

or, with printing,

printAllMoons(P) :-
  orbits(P, sun),
  findall(X, orbits(X, P), Lm),
  write(Lm).

--- EDIT ---

The OP ask

How would I combine it with outer_orbit(P,O) so that it will show all planets and its moons that are further away than P.

I suppose there are many ways.

If you want only print something and you want use outer_orbit/2, an example can be the following

printPlanetAndMoons(P1) :-
  write('- planet '), write(P1), nl,
  findall(X, orbits(X, P1), Lm),
  (     Lm \== []
     -> (write('   -- with moons '), write(Lm), nl)
     ;  true ).

printAllPlanetsAndMoonsMoreDistantThanAPlanet(P0) :-
  write('planets and moons more distant than '), write(P0), write(':'), nl,
  findall(P1, (outer_orbit(P0, P1), printPlanetAndMoons(P1)), _).

(hoping you can find better names for predicates).