0

First of all: I'm a newbie when it comes to Prolog, I just wrote my first lines yesterday.

I got the following assignment: Write a rule flatten(X,Y), which is true when

  • X is a list of lists.
  • Y is the list you get when you flatten X.

For example: flatten([[1,2,3],[],[3,4]], [1,2,3,3,4]) outputs "true".

My general idea was to take the first element of the first list from X and compare it to the first element of Y. But I don't really know how exactly I would do that...

  • `flatten( [[A|B] | C], D ) :- flatten( [A, B | C], D ).` is one rule of several that together would define such a predicate. do you understand this syntax and its meaning? if not, some tutorial studying is in order. – Will Ness Jan 29 '20 at 16:37
  • https://stackoverflow.com/tags/prolog/info – Will Ness Jan 29 '20 at 17:59
  • Sorry for late reply. I was just looking around for some sites for that tutorial studying you suggested. Now, I've taken your rule and put it in my program, with a simple flatten([], []). in front and it works. I am not 100% sure how exactly it works, but I'm currently trying to figure it out. :p – SchmongHaggage Jan 29 '20 at 18:01
  • https://stackoverflow.com/help/how-to-ask, [mcve]. – Will Ness Jan 29 '20 at 18:20
  • The name `flatten/2` is commonly used for flattening arbitrarily nested lists. See [this](https://stackoverflow.com/a/59618820/772868) in case you want `flatten([[[[1]]]],[1])` to be true. – false Jan 29 '20 at 19:27

0 Answers0