Search code examples
prologswi-prolog

Return values from database - Prolog


Good morning, I'm learning prolog. But I already searched, but I didn't find a solution, it must be very simple, but I got stuck

I created a database like the following

data base([
   movie(["Titanic"], [["Jack", "Dawson"] ,["Rose", "DeWitt", "Bukater"]], ["James","Cameron"] ),
   movie(["Hulk"], [["Bruce", "Banner"] ,["Betty", "Ross"]], ["Ang","Lee"] )
]).

This database contains the name of the film, the name of the main characters and then the director.

The goal would be to create a function:

Example

find_movies_by_character("Bruce Banner") Need return Titanic

I imagine something might be wrong, I'm open to new ideas


Solution

  • data base([ with that space in it, doesn't seem to be valid syntax in SWI Prolog. Wherever did you get that from?

    This database contains the name of the film, the name of the main characters and then the director.

    More specifically, the database contains a list containing movie terms, each containing a list containing the name of the film and (a list containing lists of parts of names of the main characters) and (a list containing parts of names of directors).

    From how complicated it is to write out, it will be at least that complicated to work with. If you can simplify it, it will get easier to work with:

    movie('Titanic', ['Jack Dawson', 'Rose DeWitt Bukater'], 'James Cameron').
    movie('Hulk', ['Bruce Banner', 'Betty Ross'], 'Ang Lee').
    
    find_movie_by_character(Character, MovieName) :-
        movie(MovieName, Characters, _),
        member(Character, Characters).
    

    e.g.

    ?- find_movie_by_character('Bruce Banner', X).
    X = 'Hulk'