prolog

arithmetic computer

纵饮孤独 提交于 2019-12-23 01:22:13
问题 I need some help in prolog, which is pretty new to me. I have to design a small arithmetic computer. The expression to be evaluated will be represented as a list for example: ?-evaluate([2,+,4,*,5,+,1,*,2,*,3],R). I am trying to do this by designing two predicates one called parse to transform my list for example: ?-parse([1,+,2,*,3],PF). PF=[+,1,[*,2,3]] and another one to evaluate the new expression. ?-evpf([+,1,[*,2,3]],R). R=7 I have problems with the first part, can anyone help my with

Prolog maze solving algorithm

北城以北 提交于 2019-12-22 14:58:45
问题 I want to implement a maze solving algorithm in Prolog. Therefore i searched for some maze solving algorithms and found the following: http://www.cs.bu.edu/teaching/alg/maze/ FIND-PATH(x, y): if (x,y outside maze) return false if (x,y is goal) return true if (x,y not open) return false mark x,y as part of solution path if (FIND-PATH(North of x,y) == true) return true if (FIND-PATH(East of x,y) == true) return true if (FIND-PATH(South of x,y) == true) return true if (FIND-PATH(West of x,y) ==

Calculate whether the sum of exactly three values in a list is equal to N

若如初见. 提交于 2019-12-22 14:43:55
问题 Examples: ([1,2,3,7,6,9], 6). should print True , as 1+2+3=6 . ([1,2,3,7,6,9], 5). should print False as there are no three numbers whose sum is 5 . ([],N) where N is equal to anything should be false. Need to use only these constructs: A single clause must be defined (no more than one clause is allowed). Only the following is permitted: + , , , ; , . , ! , :- , is , Lists -- Head and Tail syntax for list types, Variables. I have done a basic coding as per my understanding. findVal([Q|X],A) :

Calculate whether the sum of exactly three values in a list is equal to N

…衆ロ難τιáo~ 提交于 2019-12-22 14:40:42
问题 Examples: ([1,2,3,7,6,9], 6). should print True , as 1+2+3=6 . ([1,2,3,7,6,9], 5). should print False as there are no three numbers whose sum is 5 . ([],N) where N is equal to anything should be false. Need to use only these constructs: A single clause must be defined (no more than one clause is allowed). Only the following is permitted: + , , , ; , . , ! , :- , is , Lists -- Head and Tail syntax for list types, Variables. I have done a basic coding as per my understanding. findVal([Q|X],A) :

How do I show the results of pattern-matching goals in SWI-Prolog from a shell invocation?

眉间皱痕 提交于 2019-12-22 12:38:36
问题 I am wondering how one gets output from SWI-Prolog when invoking it from the shell. Say I have a simple knowledge base, kb.pl : dad(elvis, lisaMarie). dad(john, julian). I can invoke SWI-Prolog from the shell: $ swipl --quiet -s kb.pl -t listing and a listing of my knowledge base is printed to stdout . If I try this: $ swipl --quiet -s kb.pl -t "dad(elvis, X)" $ echo $? 0 No output is printed, but I know that it found matches because I get zero when I then query for the return code. Similarly

Prolog How can I construct a list of list into a single list by interleaving?

安稳与你 提交于 2019-12-22 12:32:54
问题 How can I construct a list of a list into one single list with interleaving sublists? like recons([[1,2],[3,4]],X) will give X= [1,3,2,4]? I have been trying hours and my code always gave me very strange results or infinite loop, what I thinking was something like this: recons([[A|R],REST],List):- recons(R,REST), append(A,[R|REST],List). I know its completely wrong, but I don`t know how to fix this. 回答1: Instead of thinking about efficiency first, we can think about correctness first of all.

ERROR: Out of local stack in my Prolog code

╄→尐↘猪︶ㄣ 提交于 2019-12-22 12:27:07
问题 I cannot figure out why the following query from the given Prolog code generates the error Out of local stack . Prolog code: likes(g,c). likes(c,a). likes(c,b). likes(b,a). likes(b,d). likes(X,Z) :- likes(X,Y), likes(Y,Z). the query ?- likes(g,X). results in X = c ; X = a ; X = b ; ERROR: Out of local stack Edit 1 This is the way I think that Prolog should deal with this query, likes(g,c) is a fact, so X={c} likes(g,b) <= likes(g,c) and likes(c,b), so X={c,b} likes(g,a) <= likes(g,b) and

Solving a textual logic puzzle in Prolog - Find birthday and month

让人想犯罪 __ 提交于 2019-12-22 11:35:37
问题 I'm reading the "7 Languages in 7 Days"-book, and have reached the Prolog chapter. As a learning exercises I'm trying to solve some textual logic puzzles. The puzzle goes as follow: Five sisters all have their birthday in a different month and each on a different day of the week. Using the clues below, determine the month and day of the week each sister's birthday falls. Paula was born in March but not on Saturday. Abigail's birthday was not on Friday or Wednesday. The girl whose birthday is

I am having problems getting a list of lists into a single list

空扰寡人 提交于 2019-12-22 11:35:21
问题 I am writing a solution to working out distances between numbers in a list using recursion, but have been struggling with getting the intended output. I am trying to get a list of lists into a single list, but attempts at using flatten and append/2 aren't working. I have tried for hours, and keep going around in circles, can someone tell me what i'm doing wrong please? :- use_module(library(clpfd)). difference([],_,[]). differwnce([L|Ls],X,[DST|Ds]) :- DST #= abs(X - L), difference(Ls,X,Ds).

How to run prolog queries from within the prolog file in swi-prolog?

霸气de小男生 提交于 2019-12-22 11:04:09
问题 If I have a prolog file defining the rules, and open it in a prolog terminal in windows, it loads the facts. However, then it shows the ?- prompt for me to manually type something. How can I add code to the file, so that it will actually evaluate those specific statements as if I typed them in? something like this dog.pl dog(john). dog(ben). % execute this and output this right away when I open it in the console dog(X). Does anyone know how to do this? Thanks 回答1: There is an ISO directive on