prolog

Equating a Sublist to Another Sublist for CYK table in Prolog

本小妞迷上赌 提交于 2019-12-24 01:58:33
问题 I'm currently working on a Prolog program that will generate a CYK parse table after being given a set of productions. However, I am having troubles checking two rows to see if they are equivalent. Here's what I have so far: answer(X,X). %Checks to see if it is equivalent equal(X,Y) :- sort(X,X1), sort(Y,Y1), X1 == Y1. %find the length of the lists total_length([],0). total_length([_|Xs],L) :- total_length(Xs,M), L is M+1. %storing length of lists and possible use of a decrement here to

Prolog - Split a list in two halves, reversing the first half

房东的猫 提交于 2019-12-24 01:54:48
问题 I am asked to take a list of letters into two lists that are either equal in size (even sized original list I guess) or one is larger than the other by one element (odd sized list), and reverse the first one while I'm at it. Query and output example: ?- dividelist2([a,b,c,d,e,f], L1,L2). L1 = [c,b,a] L2 = [d,e,f] ?- dividelist2([a,b,c,d,e], L1,L2). L1 = [c,b,a] L2 = [d,e] % OR L1 = [b,a] L2 = [c,d,e] I've reached this solution, but I have a feeling something is wrong with it. Please let me

Determining if a list is empty or not

微笑、不失礼 提交于 2019-12-24 01:52:07
问题 I want to do some if | else stuff in prolog. Below is my code, Prolog will return 'R = false' if my input list is not empty, it will return 'false' if my list is empty. What do I miss? Code: isEmpty([H|T],R) :- length([H|T],L), ( L > 0 -> R = 'false' ; L =:= 0 -> R = 'true' ). Prolog Output: 1 ?- isEmpty([],R). false. 2 ?- isEmpty([1],R). R = false. 3 ?- isEmpty([1,2,3],R). R = false. 回答1: One way to define this would be to use unification ("pattern matching"): list_empty([], true). list

size of tree using accumulator

放肆的年华 提交于 2019-12-24 01:48:16
问题 I am tryin to learn prolog and got stuck in one of the toy example. binary_tree is defined as: The term '-' (the minus symbol) represents the empty tree. The term t(L,V,R) represents a tree with left subtree L, node value V, and right subtree R. now Im trying to write the predicate size(Tree,N) to find the size of tree. I know it is possible using the following : size( -,0). size( t(L, _,R), S) :- size(L,Ls), size(R,Rs), S is Ls + Rs + 1. but I want to make it work using accumulator. I tired

Binary to decimal - prolog

泄露秘密 提交于 2019-12-24 01:47:29
问题 I found this on stack: reversible "binary to number" predicate But I don't understand :- use_module(library(clpfd)). binary_number(Bs0, N) :- reverse(Bs0, Bs), binary_number(Bs, 0, 0, N). binary_number([], _, N, N). binary_number([B|Bs], I0, N0, N) :- B in 0..1, N1 #= N0 + (2^I0)*B, I1 #= I0 + 1, binary_number(Bs, I1, N1, N). Example queries: ?- binary_number([1,0,1], N). N = 5. ?- binary_number(Bs, 5). Bs = [1, 0, 1] . Could somebody explain me the code Especialy this : binary_number([], _,

Einstein's riddle

坚强是说给别人听的谎言 提交于 2019-12-24 01:39:26
问题 I am new to Prolog and I'm trying to model a riddle like the Einstein riddle, also known as the Zebra riddle, (but with 10 houses and 30 hints) in Prolog and I'm using this model example as a starting point: http://www.baptiste-wicht.com/2010/09/solve-einsteins-riddle-using-prolog/ But in my riddle, i have to be able to say that X is right of Y. And I don't mean directly right, but right of in the list. So Y can be in house 1, while X in house 9. How can I do this in Prolog? I was thinking

How to correctly filter a clause that returns multiple duplicated values?

…衆ロ難τιáo~ 提交于 2019-12-24 01:00:18
问题 I'm trying to correctly filter the values returned from a clause (it is returning multiple duplicated values). I'm having a hard time understanding the logic programming, sorry if its an stupid question. These are my facts/predicates: home(peter, sanFrancisco, 1000). home(ash, sanFrancisco, 100). home(juan, sanFrancisco, 400). home(juan, california, 700). home(ash, california, 600). home(peter, california, 500). home(peter, vegas, 100). home(ash, vegas, 80). home(juan, vegas, 60). What im

What does a hyphen at end of a term mean

自闭症网瘾萝莉.ら 提交于 2019-12-24 00:58:10
问题 I'm trying to understand the statement and I can't find any thing about the -1/4 at the the end of the object term. I've tried searching but I'm not even sure what to search for. exists(A,object(B,A,apple,countable,na,eq,1)-1/4). 回答1: The second argument of exists/2 are two terms in pair notation. One term being object(_A,A,apple,countable,na,eq,1) and the other being the 1/4 . You can see this if you try the following query: ?- exists(A,X-Y). X = object(_A,A,apple,countable,na,eq,1), Y = 1/4

how to use subexpressions

社会主义新天地 提交于 2019-12-24 00:55:11
问题 Hello stackoverflow community, i have a prolog code that creates subexpressions for a truth table and then gives them values true/false well its working fine for alot of expressions but using this statement this happens 1 ?- phrase(subexprs(bicond(and(impl(A,B),impl(B,A)),bicond(A,B))),Subexprs), bindList([A,B]), maplist(expr_value, Subexprs, Values). A = B, B = true, Subexprs = [true impl true, true impl true, (true impl true)and(true impl true), true bicond true, **(true impl true)and(true

Using a self-created list in prolog

那年仲夏 提交于 2019-12-24 00:46:49
问题 I'm pretty new to Prolog, Don't be too hard on me. Anyhow, I've got the next problem in Prolog: I've created a small 'database' of actors, defined by: actor(ID, Name). Same goes for movies,cast,Director, Defined by: movie(ID,Name, Director, Category). director(ID,Name). cast(MovieID, ActorID). Now i need to Write a procedure people(Movie, List) that defines the relation between movie name and list of names of all people participated in the movie - director and actors (in any order). So I've