prolog

Prolog List Question

为君一笑 提交于 2019-12-24 19:33:38
问题 I'm trying to understand lists in prolog when I stumbpled over this problem: there's a predicate mergeandmap/2 that should basically do this: mergeandmap([[a1,...,an],...,[z1,...,zm]],[x1...xn]) %----------list 1------------ -list 2-- List 2 consits of letters (for example [a,b,c]). List 1 consits of several lists with size(list2) elements containing 1s and 0s (for example: [[0,0,1],[1,0,1],[0,1,1]]) The prolog program should determine from list 1 which elements from list 2 should be printed

Generate random number of 5 digits in Prolog

こ雲淡風輕ζ 提交于 2019-12-24 19:33:27
问题 I want to generate a random number of 5 digits using assert/1 and retract/1. I already have the following code, but I don't know how to generate a number with 5 digits. draw :- % length(X, 5), random_between(0, 99999, X), assert(rnumber(X)), write(X). Thank you! 回答1: You could just do the random predicate 5 times, then put it in a list. Something like this: code(X):- random(0,9,Elem1), random(0,9,Elem2), random(0,9,Elem3), random(0,9,Elem4), random(0,9,Elem5), X = [Elem1, Elem2, Elem3, Elem4,

Prolog date_time_stamp [closed]

吃可爱长大的小学妹 提交于 2019-12-24 19:28:07
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Trying to make a rule within prolog, which would ask if any messages where sent between 2 individuals (where both individuals can be the recipient or the sender of the message) before a given date. After calling whether it is true or not, it is to print all the logs where the 2 users have exchanged

Removing Duplicate Elements from List of Lists in Prolog

吃可爱长大的小学妹 提交于 2019-12-24 18:53:06
问题 I am trying to work out how to remove duplicate elements in a list of lists in Prolog. E.g: input: [[1,2,3],[5,6],[3,4],[1,7]] expected output: [[1,2,3],[5,6],[4],[7]] I know I can use the predicate sort/2 to remove duplicates in a single list, but how do I get it to work across multiple lists? 回答1: Here is my attempt. Hope you've made some attempts to solve it and learn from this...Anyway if you still didn't come up with anything take a look at the following code: remove_dupl(InL, OutL):-

How to use Prolog with PHP?

南笙酒味 提交于 2019-12-24 18:50:29
问题 I want to use Prolog with PHP. Is it possible? 回答1: There are always the exec-familiy functions to execute/spawn another process. 回答2: As suggested above, you can execute a Prolog interpreter or binary. However, most Prolog implementations also export a C API that can be used to call the Prolog interpreter. You could create a small PHP module to start an interpreter and execute queries. For instance, the SICStus documentation describes using Prolog from C in detail: 6 Mixing C/C++ and Prolog

SWI Prolog if statements, how do they work? Generating a simple grid

蓝咒 提交于 2019-12-24 18:38:25
问题 I realize I've edited out the if statements out of the original code which doesn't help readability and question clarity. Just skip to the answers for the explanation on how they work with a small example program. To learn about more complex programs using if statements in Prolog, I'm creating a simple platformer that generates some objects and places them in a grid. First I'm trying to generate a simple 'world' with the idea of trying out generating things in prolog. The plan is to create a

What the the variables in following program stores?

。_饼干妹妹 提交于 2019-12-24 18:23:33
问题 total_item([],0). total_item([_|Rest],N) :- total_item(Rest,C), N is C+1. This program takes an array as input and count the total number of item in that list. But I am not getting what exactly variable N and C stores. Any explanation would be appreciate. Thank you 回答1: Read it backwards: total_item([], 0). total_item maps 0 to [] ( relates the two). total_item([_ | Rest], N) :- total_item( Rest, C ), N is C + 1 . with C mapped to Rest , C+1 is mapped to [_ | Rest] -- the list one element

definite clause grammar (dcg) Prolog (homework)

孤街浪徒 提交于 2019-12-24 18:13:15
问题 i tried to write a predicate and N={Expression,Number,Digit,Operator,Variable} T={1,2,3,+,-,*,(,),X,Y,Z} and S is expression and program p defines as Expression-->Number **Expression-->(Expression) Operator (Expression)** Number-->Digit **Number --> Digit Number** Digit-->1 Digit-->2 Digit-->3 Operator-->+ Operator-->- Operator-->* Variable-->X Variable-->Y Variable-->Z I think that i implemented many parts however could not implement bold parts!! my prolog code that describes the terminals

not member rule in prolog doesn't work as expected

女生的网名这么多〃 提交于 2019-12-24 16:42:37
问题 I'm trying to write a simple maze search program in prolog, before I add a room to visited list I'm checking whether it is already a member of the visited list. However, I can't get this to work, even if I use the code from the book: d(a,b). d(b,e). d(b,c). d(d,e). d(c,d). d(e,f). d(g,e). go(X, X, T). go(X, Y, T) :- (d(X,Z) ; d(Z, X)), \+ member(Z,T), go(Z, Y, [Z|T]). What do I do wrong? 回答1: Your program seems to be ok. I guess the problem is that you are calling go/3 with the third argument

Prolog: Generating Every Possibility of a List Given a Pattern

久未见 提交于 2019-12-24 16:14:48
问题 Let's say you have a list in Prolog such as: [3,4,2,2,1,4]. How would one go about generating a list of lists of all possible patterns that start at the first element of the list, then either go to the i + 2th element, or the i + 3rd element, and so on from there. Example: Say I have [3,4,2,2,1,4,8]. I want to be able to generate a list of lists such as: [[3,2,1,8], [3,2,4], [3,2,8]] I.e. all possibilities of either every other element or every i+3 element, or any other combination, such as i