简体   繁体   中英

prolog predicate returns facts(?)

First of all, if the answer to my question is here or here I couldn't find it so please don't kill me.

I want to write a Prolog predicate which returns a list of this form:

(list[elem, elem2], list[elem3, elem4], list[elem5, elem6]).  

now I can think of several ways to return a list of this form:

([elem, elem2], [elem3, elem4],[elem5, elem6]).

but how do I make the word "list" appear there as well? what is it even? a fact? another predicate?

Thanks in advance!

You can create a compound term using the standard =../3 built-in predicate. For example:

| ?- Term =.. [list, [1,2,3]].

Term = list([1,2,3])
yes

But note that the syntax that you're trying to use, list[elem5, elem6] , is not valid. Are you trying to mimic an array representation? If so, maybe use instead list(elem5, elem6) ? For example:

| ?- Term =.. [list, elem5, elem6].

Term = list(elem5, elem6)
yes

This

(list[elem, elem2], list[elem3, elem4], list[elem5, elem6]).  

is invalid in Prolog.

[] means that it is a list, so it can not be a fact or predicate, which means that you can not put a functor name before [ .

(A,B,C) is known as a tuple in other programming languages or a structure in Prolog.

If you wanted to use multiple list in a structure in Prolog you would think of it as

(A,B,C)

where A,B and C can be list.

Examples with values

([],[],[])
([a],[b],[c])
([],[a,b,c],[d,e,g])

or for your specific case

([elem1,elem2],[elem3,elem4],[elem5,elem6])

You could also create a compound term

list(A,B,C)

Examples with values

list([],[],[])
list([a],[b],[c])
list([],[a,b,c],[d,e,g])
list([elem1,elem2],[elem3,elem4],[elem5,elem6])

Remember that a list is actually terms combined with the cons or ./2 operator and is syntactic sugar . This can be seen by using write_canonical/1 with GNU Prolog.

?- write_canonical(([1,2],[3,4],[5,6])).

','('.'(1,'.'(2,[])),','('.'(3,'.'(4,[])),'.'(5,'.'(6,[]))))

or

?- write_canonical(list([1,2],[3,4],[5,6])).

list('.'(elem1,'.'(elem2,[])),'.'(elem3,'.'(elem4,[])),'.'(elem5,'.'(elem6,[])))

When you try to use the word list as a functor for a list [] , eg list[a,b] you can not because a list already has the functor . . It is the syntactic sugar that hides the functor . . The reason it is easy for some of us to remember this is that in languages such as Lisp they still use cons or when the syntactic sugar for list is not known to the parser, then you learn quickly how to use cons .

After comment by OP

it should be [list(elem, elem1), list(elem2, elem3)]

Now you have a list [] with the list terms being compound terms of list(X,Y)

That is valid Prolog.

write_canonical([list(elem1,elem2),list(elem3,elem4),list(elem5,elem6)]).

'.'(list(elem1,elem2),'.'(list(elem3,elem4),'.'(list(elem5,elem6),[])))

or to make the . operator stand out as

'.'(                          
    list(elem1,elem2),          
   '.'(                         
       list(elem3,elem4),
       '.'(
           list(elem5,elem6),
           []
          )
      )
   )

Notice that for each . there are two arguments. The arguments can be a structure or compound structure such as list(elem1,elem2) , another . with two arguments, or the empty list, [] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM