Def of takeout()

79 views
Skip to first unread message

sovereig...@outlook.com

unread,
Nov 12, 2016, 8:45:25 AM11/12/16
to SWI-Prolog
takeout(X,[X|R],R).
takeout(X,[F|R],[F|S]) :- takeout(X,R,S).

Above is the definition of takeout predicate.
Can't understand the below description upon this def.

"When X is taken out of [X|R], R results.

 When X is taken out of the tail of [X|R], [X|S] results, where S is the result of taking X out of R."

Q. what is the meaning of "R results."? for example, if we take out 1 from [1,2,3], it becomes [2,3]. it is not R. 


Boris Vassilev

unread,
Nov 12, 2016, 9:32:09 AM11/12/16
to sovereig...@outlook.com, SWI-Prolog
This predicate definition is identical to the library predicate `select/3`. You can see its documentation and implementation here:

http://eu.swi-prolog.org/pldoc/doc/home/swipl/lib/swipl/library/lists.pl?show=src#select/3

Trying to formulate in English (or any other natural language) the operational meaning of Prolog predicates has been an endless source of confusion for me. It might be easier to look at the definition, maybe try to work out what the proof tree would look like for a few simple queries.


Save our in-boxes! http://emailcharter.org

--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.

Feliks Kluzniak

unread,
Nov 12, 2016, 12:09:10 PM11/12/16
to sovereig...@outlook.com, SWI-Prolog
The key here is that  [1, 2, 3 ] is just syntactic sugar for [1 | [ 2 | [ 3 | [] ] ].

So if you unify [1, 2, 3 ] with [ X | R ] ,  X becomes instantiated to 1 and R to [ 2 | [ 3 | [] ] ], i.e.,
[ 2, 3 ], and R indeed „results”.

Hope this helps,
— Feliks

--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.

Anne Ogborn

unread,
Nov 12, 2016, 4:03:47 PM11/12/16
to Feliks Kluzniak, sovereig...@outlook.com, SWI-Prolog
Prepared food that is sold, ready to eat or nearly so,
for consumption at home or another location.

::Annie is here, with the Chinese takeout. She gives Boris first crack at the potstickers::
Reply all
Reply to author
Forward
0 new messages