2

I'm doing a bunch of Prolog tasks and I have very little idea of how lists work. I have the following:

is_a_list(+L). Test whether L is a valid Prolog list.

How can I write a predicate for that? What does the + mean?

peter.cyc
  • 1,763
  • 1
  • 12
  • 19
Haise Sasaki
  • 319
  • 3
  • 9
  • 1
    see https://www.swi-prolog.org/pldoc/man?section=modes. it says *"+ : Argument is fully instantiated at call-time, to a term that satisfies the type. This is not necessarily ground, e.g., the term `[_]` is a list, although its only member is unbound."* i.e. you can expect calls like `is_a_list( [1,2,3] )`, `is_a_list( [1, B, 3] )`, `is_a_list( [1 | T] )`, but not `is_a_list( no )`. so how to write it? start with the first three clauses exactly as I've given them, and [*Generalize!*](https://stackoverflow.com/search?q=user%3A849891+%5Bprolog%5D+generalize). – Will Ness Nov 09 '20 at 16:52
  • can you point me towards a resource that explains lists? – Haise Sasaki Nov 09 '20 at 16:54
  • https://stackoverflow.com/search?q=+%5Bprolog%5D+lists+is%3Aq , e.g. https://stackoverflow.com/q/11539203/849891, https://stackoverflow.com/a/10620282/849891. – Will Ness Nov 09 '20 at 16:59
  • 1
    Also http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlch4 – David Tonhofer Nov 09 '20 at 16:59
  • 1
    @WillNess I like this "Generalize!" resource – David Tonhofer Nov 09 '20 at 17:01
  • @DavidTonhofer thanks, it's nice to hear. I came to it gradually, I think. there should be something, there, I'm inching toward it at ever slowing pace. :) -- if a "smart compiler" were made to work that way, we'd perhaps hit the trade-off between brevity and typo detection. i.e. if the compiler tries to make sense of what we've written, it'd have to assume we made no typos. and if we had, it'd still make sense of it but it would probably be nonsensical. :) now, compilers just fail us on the simplest of typos, make us chase them ourselves, and also write out the definitions in full, ourselves. – Will Ness Nov 09 '20 at 17:11
  • https://www.swi-prolog.org/pldoc/doc_for?object=is_list/1 This is all you should need to implement it. **More important** is what is really supposed to happen in edge cases. – TA_intern Nov 10 '20 at 12:30
  • You need to make your question more specific. While it can be taken that you probably mean closed list (because you asked the question) you should state if an open list is considered valid. As such your question and possibly the answers based on your imprecise question will just confuse people who don't know the difference. – Guy Coder Nov 10 '20 at 13:01
  • See this [post](https://swi-prolog.discourse.group/t/is-there-a-has-type-2-for-difference-list/2622/3?u=ericgt) in the SWI-Prolog forum. You will need to click the triangle to reveal the code. – Guy Coder Nov 10 '20 at 13:03

1 Answers1

2

The following can be simplified to give You the solution You seek .

is_a_list_of_integer([]) .

is_a_list_of_integer([ITEM|LISTs])
:-
prolog:integer(ITEM) ,
is_a_list_of_integer(LISTs)
.

Kintalken
  • 763
  • 5
  • 9