prolog

Prolog Remove the elements between the first and the last elements in a list

我的梦境 提交于 2019-12-31 05:11:11
问题 I am trying to keep only the first element and the last element for a list which contains only consecutive integers. For example: ?- remove([1,2,3,4,5], NewList). NewList = [1,5]. I can only successfully keep the last element: remove([], []). % for consecutive integers in the list remove([ Head | Tail ], NewList) :- check_consecutive(Head, Tail), remove(Tail, NewList). % for the case when the list is empty remove([ Head | Tail ], [ Head | NewList ]) :- not(check_consecutive(Head, Tail)),

convert string to list in prolog

爷,独闯天下 提交于 2019-12-31 04:15:13
问题 I am a Prolog newbie and am stuck at parsing a string to a list. I have a string of the form 1..2...3..4 I wish to convert it into a list which looks like [1, _, _, 2, _, _, _, 3, _, _, 4] How can I achieve this functionality? 回答1: Another solution is to use DCG's. The code is straightforward: digit(N) --> [ D ], { member(D, "0123456789"), number_codes(N, [D]) }. dot(_) --> ".". token(T) --> digit(T). token(T) --> dot(T). tokens([T|Ts]) --> token(T), tokens(Ts). tokens([]) --> "". parse_codes

Issues with storage of facts in Prolog

喜夏-厌秋 提交于 2019-12-31 03:59:10
问题 I'm kinda new in Prolog and I'm using SWI-Prolog v6.6 to storage asserts in my *.pl file. :- dynamic fact/2. assert(fact(fact1,fact2)). With the code above I can make asserts and it works fine, but the problem is when I close SWI-Prolog and I open the *.pl file again, the asserts i've made are gone... There is a way to make asserts and those get stored even if I close the SWI-Prolog? Sorry about my bad english and Thanks! (: 回答1: Saving state has certain limitations, also see the recent

Prolog recursively count numbers in a list

那年仲夏 提交于 2019-12-31 03:51:38
问题 I need a program to count all the numbers in a list, no matter how DEEPLY NESTED they are. I was able to count numbers in the case where they were not inside another list, but recursing through deeply nested elements is not working out. I have this so far: count([],0). count([H|Tail], N) :- count(Tail, N1), ( number(H) ->N is N1 + 1 ; is_list(H) -> count(H,N) ; N = N1 ). So, if I were to call count([a,1,[2,b],3],N) , the output should be N=3 ; however, I only get N=2 . Could someone please

Using Prolog to compute the GCD of a polynomial

和自甴很熟 提交于 2019-12-31 03:51:25
问题 The title kind of says it all. I'm looking to compute the GCD of two polynomials. Is there any way this can be done in Prolog? If so, what's a good starting point? Specifically, I'm having trouble with how to implement polynomial division using Prolog. Edit to include example input and output: Example input: ?- GCD(x^2 + 7x + 6, x2 − 5x − 6, X). Example output: X = x + 1. Solution On the off chance that someone else needs to do this, here's my final solution: tail([_|Tail], Tail). head([Head

Einstein Riddle using Prolog

余生长醉 提交于 2019-12-31 03:17:08
问题 I'm trying to solve the Einstein riddle using Prolog. When I'm trying to run by houses(Hs), it shows No. Task is The Brit lives in the red house. The Swede keeps dogs as pets. The Dane drinks tea. The green house is on the immediate left of the white house. The green house's owner drinks coffee. The owner who smokes Pall Mall rears birds. The owner of the yellow house smokes Dunhill. The owner living in the center house drinks milk. The Norwegian lives in the first house. The owner who smokes

Reading a file in prolog [duplicate]

荒凉一梦 提交于 2019-12-31 02:57:29
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Read a file line by line in Prolog I found the following prolog code which reads one character at a time and prints out. process(File) :- open('C:/Users/BHARAT/Desktop/a.txt', read, In), get_char(In, Char1), process_stream(Char1, In), close(In). process_stream(end_of_file, _) :- !. process_stream(Char, In) :- print(Char), get_char(In, Char2), process_stream(Char2, In). But if the file has multiple lines is there

Difference between X\=Y and dif(X,Y)

人盡茶涼 提交于 2019-12-31 01:55:10
问题 What is the difference between this: X \= Y and this piece of code: dif(X, Y) I thought that they should behave the same, but they do not. Here's the example: n_puta(L, N, X) :- nputa(L, N, 0, X). nputa([], N, C, _) :- N = C. nputa([G|R], N, C, X) :- G = X, nputa(R, N, Y, X), C is Y - 1. nputa([G|R], N, C, X) :- dif(G,X), nputa(R, N, C, X). And here are some calls: ?- n_puta([a,a,b,b,b], 2, X). X = a ; false. ?- n_puta([a,a,b,a,b,b], 3, X). X = a ; X = b ; false. X should be the atom that

Generalizing Fibonacci sequence with SICStus Prolog

我的未来我决定 提交于 2019-12-31 01:00:15
问题 I'm trying to find a solution for a query on a generalized Fibonacci sequence (GFS). The query is: are there any GFS that have 885 as their 12th number? The initial 2 numbers may be restricted between 1 and 10. I already found the solution to find the Nth number in a sequence that starts at (1, 1) in which I explicitly define the initial numbers. Here is what I have for this: fib(1, 1). fib(2, 1). fib(N, X) :- N #> 1, Nmin1 #= N - 1, Nmin2 #= N - 2, fib(Nmin1, Xmin1), fib(Nmin2, Xmin2), X #=

Remove unique elements only

蓝咒 提交于 2019-12-31 00:37:21
问题 There are many resources on how to remove duplicates and similar issues but I can't seem to be able to find any on removing unique elements. I'm using SWI-Prolog but I don't want to use built-ins to achieve this. That is, calling remove_unique([1, 2, 2, 3, 4, 5, 7, 6, 7], X). should happily result in X = [2, 2, 7, 7] . The obvious solution is as something along the lines of count(_, [], 0) :- !. count(E, [E | Es], A) :- S is A + 1, count(E, Es, S). count(E, [_ | Es], A) :- count(E, Es, A). is