prolog

Prolog Path Search Pointers

I need to check if a path is valid, true or false. It's given like this: ?-path(a,b,[(a,c),(c,d),(d,b)]). true In the list part, how do I access the a or c in (a,c)? Is it like a string"(a,c)"? And in general how would one solve this type of path finding? Sample code/pseudo is appreciated. Is there a way to make it interpret the tup...

Prolog looping though variable results

Hello, I have a small program wrote in prolog. At the moment I can print the first result with test(X, 1, 4, 5). write(X). But if there is more than one result for X, How do I print the next ones? Thanks ~ KyleG ...

Max out of values defined by prolog clauses

I know how to iterate over lists in Prolog to find the maximum, but what if each thing is a separate clause? For example if I had a bunch of felines and their ages, how would I find the oldest kitty? cat(sassy, 5). cat(misty, 3). cat(princess, 2). My first thought was "hmm, the oldest cat is the one for which no older exists". But I c...

What does \+ means in Prolog?

I've seen some answers here that use it and I don't know what it means or how to use it. I's also hard to look for it via a search engine :) ...

Prolog parse postfix math expressions

I solved this my self. I'll post the solution when were past due date for my homework. Okay, I'm going to build a parser or an evaluator. The de facto standard when parsing with prefix notation is to just use a stack. Add to the stack if input is a number, if it is an operator you can pop twice apply operator and put the result back on...

Prolog mystery(c,[a,b,c,d],Z).

I think the answer is 3 but I am not sure, can anyone provide some help? Suppose the following two statements are entered into Prolog: mystery(X,[X|L],L). mystery(X,[Y|L],[Y|M]) :- mystery(X,L,M). What would Prolog return if one then gives it the following goal? ?- mystery(c,[a,b,c,d],Z). ...

Uses of non-ground facts in Prolog?

In Prolog you can write a ground fact as: lost(jen). You can also write a non-ground fact as: lost(X). Does this makes any sense? Could you show me a practical/real example where non ground facts are used? Thanks, ...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [H|T], [H|Tnew]) :- insert_pq(State, T, Tnew). precedes(X, Y) :- X < Y. % < needs to be defined depending on problem the function qui...

Prolog Question

hill(+IntList) succeeds if IntList consists of monotonically increasing integers followed by monotonically decreasing integers. For example, [1,2,5,8,11,6,3,-1] is a hill, but [1,2,5,8,11,6,9,3,-1] and [1,2,3,4,5,6] are not hills. You may assume that IntList contains only integers. This is what i have done so far. hill(List) :- incr...

Pascal's Triangle in Prolog

I have written a function for returning the next row in Pascal's Triangle given the current row: pascal_next_row([X],[X]). pascal_next_row([H,H2|T],[A|B]):- pascal_next_row([H2|T],B), A is H + H2. I want to be able to find the nth row in the triangle, e.g. pascal(5,Row), Row=[1,5,1,0,1,0,5,1]. I have this: pascal(N,Row):- pa...

prolog pascal triangle

hi is there anybody know how can i do the pascal nth row when i ask for :? pascal(2,Row). i get Row=[1,2,1] ?? please help me ...

Prolog - member predicate one-liner

Interview question! This is how you normally define the member relation in Prolog: member(X, [X|_]). % member(X, [Head|Tail]) is true if X = Head % that is, if X is the head of the list member(X, [_|Tail]) :- % or if X is a member of Tail, member(X, Tail). % ie. if member(X, Tail) is true. D...

How to determine whether two list have same element in prolog?

How to determine whether two list have same element in prolog? If i have two list A and B, i want to know whether they have the same element. ...

SWI-Prolog conditional statements

I'm trying to write a function that will test to see if the word hello is contained in a list. If it is contained, i don't want it to say "true", i want it to say : "yes, the word hello is contained here", any ideas? Here's my code : contains_hello([hello|_]). contains_hello([Head|Tail]):- Head \= hello, contains_hello(Tail). ...

Given [1,2,3] in prolog get back [6,5,3] by reverse accumalation

Q. Given [1,2,3] in prolog get back [6,5,3] by reverse accumalation I have the start code: accumalate([H],[H]). accumalate([H1 | H2], [Hnew, H2]), Hnew is H1 + H2. .... I am looking for basic prolog solution. thanks guys ...

Can you get a job thanks to your Prolog skills?

This Prolog question is introduced as an interview question. Can you get a job thanks to your Prolog skills? Is it used in the industry? (ok, ok, a job can be out of the industry too). ...

prolog - why this strange trace

here is the prolog code (which i sort of follow). len([],0). len([_|T],N) :- len(T,X), N is X+1. and here is the trace for it (im running linux, swi) [trace] ?- len([d,f,w,c],X). Call: (7) len([d, f, w, c], _G314) ? Call: (8) len([f, w, c], _L182) ? Call: (9) len([w, c], _L201) ? Call: (10) len([c], _L220) ? C...

Prolog : Learning by example

I am trying to learn a little bit about swi-prolog (beyond the basic, useless programs). Can anyone explain (perhaps in pseudocode) what this sudoku solver and the related functions are doing? If you need more reference it is found in the CLP(FD) package of swi-prolog. Thanks! | sudoku(Rows) :- ...

substitute in a nested list (prolog)

/* substitute(X,Y,Xs,Ys) is true if the list Ys is the result of substituting Y for all occurrences of X in the list Xs. This is what I have so far: subs(_,_,[],[]). subs(X,Y,[X|L1],[Y|L2]):- subs(X,Y,L1,L2). subs(X,Y,[H|L1],[H|L2]):- X\=H, not(H=[_|_]), subs(X,Y,L1,L2). subs(X,Y,[H|_],[L2]):- X\=H, H=[_|_], subs(X,Y,H,L2). My code w...

What's the -> operator in Prolog and how can I use it?

I've read about it in a book but it wasn't explained at all. I also never saw it in a program. Is part of Prolog syntax? What's it for? Do you use it? Thanks ...