Search code examples
prolog

How to parse the first part of a Text in Prolog?


Given a database like this

birthday(daniel,2018-11-22).
birthday(paul,2000-01-30).

I need to find all persons that have a birthday in the year 2018. I have no idea how to do that. I'm thinking about something like regular expressions or string methods from other programming languages. So it would be something like startswith(2018-11-22,2018) => true.


Solution

  • You dont need to parse text as your dates are terms of the canonical form -(-(Year, Month), Day) so you can just issue birthday(Person, 2018-_-_) to get upon backtracking all the persons with birthday in 2018.

    To collect all the persons in a list, you may use findall/3 like this:

    findall(Person, birthday(Person, 2018-_-_),  Persons).