3

I am currently stuck on a prolog problem.

So far I have:

film(Title) :- movie(Title,_,_). (Where 'movie(T,_,_,)' is a reference to my database)

namesearch(Title, Firstword) :- film(Title), contains_term(Firstword, Title).

It is hard to explain what I need help on, but basically is there a wildcard I can use to search for all films starting with a specific word, for example, if I were to search for all films beginning with the word "The".

Is there a wildcard which would allow me to input as such: namesearch(X,'The*') ?

I have tried using the asterisk like this and it does not work,

Thank your for your help

1 Answers1

4

It all depends how the title is represented.

Atom

If it is represented as an atom, you need sub_atom(Atom, Before, Length, After, Sub_atom)

?- Title = 'The Third Man', sub_atom(Title, 0, _, _, 'The').
   Title = 'The Third Man'.

List of codes

If it is a list of codes which is called a string in Prologs in Edinburgh tradition, you can either "hard code" it with append/3 or you might use Definite Clause Grammars for general patterns.

?- set_prolog_flag(double_quotes,codes).
   true.
?- append("The",_, Pattern), Title = "The Third Man", Pattern = Title.
   Pattern = Title, Title = [84,104,101,32,84,104,105,114,100|...].
?- Title = "The Third Man", phrase(("The",...), Title).
   Title = [84,104,101,32,84,104,105,114,100|...]
;  false.

Note that 84 is the character code of T etc.

phrase/2 is "the entry" to grammars. See for more. Above used the following definition:

... --> [] | [_], ... .

List of characters

Similar to list of codes, list of characters provide a more readable representation that has still the advantages of being compatible with list predicates and Definite Clause Grammars:

?- set_prolog_flag(double_quotes,chars).
   true.
?- append("The",_, Pattern), Title = "The Third Man", Pattern = Title.
   Pattern = Title, Title = ['T',h,e,' ','T',h,i,r,d|...].

?- Title = "The Third Man", phrase(("The",...), Title).
   Title = ['T',h,e,' ','T',h,i,r,d|...]
;  false.

See also this answer.

false
  • 10,264
  • 13
  • 101
  • 209
  • Eh... you beat me to suggesting `sub_atom/4` by 3 minutes (+1). :) – lurker Jan 06 '15 at 20:45
  • @lurker: Why don't you answer [my question](http://stackoverflow.com/q/27306453/772868)? It is waiting here since a month, I wasted a total of 400 on bounties on it (that is, there was nobody to award 400 rep...). – false Jan 06 '15 at 21:05
  • for some reason, I didn't see that one. Apologies. I'll have a look at it. – lurker Jan 06 '15 at 21:10