prolog

How to get the first, middle and last element of a list scheme and prolog?

家住魔仙堡 提交于 2020-01-04 13:54:09
问题 I am trying to write a function in Scheme and Prolog that returns first, middle and last item of a list. E.g., find([4,5,8,7,9],L), L = [4,8,9] . I came up with this piece of code for Scheme language, but I am new to Prolog and don't know much, so how I can get the same result in Prolog? (define (frst L) (car L)) (define (last L) (if (null? (cdr L)) (car L) (last (cdr L)))) (define (nth L x) (if (= x 1) (car L) (nth (cdr L) (- x 1)))) (define (firstMidLast L) (list (frst L) (nth L (ceiling (/

SWI-Prolog: Understanding Infinite Loops

主宰稳场 提交于 2020-01-04 13:45:53
问题 I am currently trying to understand the basics of prolog. I have a knowledge base like this: p(a). p(X) :- p(X). If I enter the query p(b) , the unification with the fact fails and the rule p(X) :- p(X) is used which leads the unification with the fact to fail again. Why is the rule applied over and over again after that? Couldn't prolog return false at this point? After a certain time I get the message "Time limit exceeded". I'm not quite sure why prolog uses the rule over and over again,

SWI-Prolog: Understanding Infinite Loops

邮差的信 提交于 2020-01-04 13:45:16
问题 I am currently trying to understand the basics of prolog. I have a knowledge base like this: p(a). p(X) :- p(X). If I enter the query p(b) , the unification with the fact fails and the rule p(X) :- p(X) is used which leads the unification with the fact to fail again. Why is the rule applied over and over again after that? Couldn't prolog return false at this point? After a certain time I get the message "Time limit exceeded". I'm not quite sure why prolog uses the rule over and over again,

Count number of matching elements in two lists

六眼飞鱼酱① 提交于 2020-01-04 11:12:45
问题 I have 2 lists with random number of elemets. Eg A=[1,2,4,5] and B=[1,2,3]. Result should be 2. Code that I tried: domains Numbers1 = integer* Numbers2 = integer* int_list=integer* predicates nondeterm prinadl(integer, int_list) clauses //here going the code that read number that I've entered, and according to entered numer,programm should do something answer(T):- T=5, P = 0, write ("Enter the 1st list"), readterm (int_list, L), write ("Enter the 2nd list"), readterm (int_list, L2), L2 = [H|V

Count number of matching elements in two lists

◇◆丶佛笑我妖孽 提交于 2020-01-04 11:11:35
问题 I have 2 lists with random number of elemets. Eg A=[1,2,4,5] and B=[1,2,3]. Result should be 2. Code that I tried: domains Numbers1 = integer* Numbers2 = integer* int_list=integer* predicates nondeterm prinadl(integer, int_list) clauses //here going the code that read number that I've entered, and according to entered numer,programm should do something answer(T):- T=5, P = 0, write ("Enter the 1st list"), readterm (int_list, L), write ("Enter the 2nd list"), readterm (int_list, L2), L2 = [H|V

How to use a “long int” in GNU Prolog?

眉间皱痕 提交于 2020-01-04 09:03:58
问题 So basically it seems that GNU Prolog use 28bit integer on my 32bit x86 Linux. The code below can not be compiled: foo(A) :- A0 is 0xdeadbeef, A1 is A0 >> 8, A2 is A0 >> 16, A3 is A0 >> 24. Then I am confused with two typical situations below: How to represent a 32bit integer in GNU Prolog (like 0xdeadbeef)? and pushing it further, how to represent a 64 bit integer ? On 64 bit x86 Linux, long long type in C has 64 bit. When using GNU Prolog to call C, the interface defined in the manual has

How to use a “long int” in GNU Prolog?

此生再无相见时 提交于 2020-01-04 09:03:29
问题 So basically it seems that GNU Prolog use 28bit integer on my 32bit x86 Linux. The code below can not be compiled: foo(A) :- A0 is 0xdeadbeef, A1 is A0 >> 8, A2 is A0 >> 16, A3 is A0 >> 24. Then I am confused with two typical situations below: How to represent a 32bit integer in GNU Prolog (like 0xdeadbeef)? and pushing it further, how to represent a 64 bit integer ? On 64 bit x86 Linux, long long type in C has 64 bit. When using GNU Prolog to call C, the interface defined in the manual has

How to use a “long int” in GNU Prolog?

假装没事ソ 提交于 2020-01-04 09:03:12
问题 So basically it seems that GNU Prolog use 28bit integer on my 32bit x86 Linux. The code below can not be compiled: foo(A) :- A0 is 0xdeadbeef, A1 is A0 >> 8, A2 is A0 >> 16, A3 is A0 >> 24. Then I am confused with two typical situations below: How to represent a 32bit integer in GNU Prolog (like 0xdeadbeef)? and pushing it further, how to represent a 64 bit integer ? On 64 bit x86 Linux, long long type in C has 64 bit. When using GNU Prolog to call C, the interface defined in the manual has

AllegroGraph check existing triple

白昼怎懂夜的黑 提交于 2020-01-04 08:00:27
问题 I am using AllegroGraph 4. I have a triple store, and I am trying to add new triples only if they don't already exist. Here is my Prolog query: (select (?news) (alfas ?news) (a-- ?news !tst:has-annotation !tst:Test))) where alfas checks for a condition(it works fine) and a-- is defined like this: (<-- (a-- ?s ?p ?o) ;; Fails unless all parts ground. (lisp (not (triple-exists-p ?s ?p ?o))) (lisp (add-triple ?s ?p ?o))) I have also tried defining it like this: (<-- (a-- ?s ?p ?o) ;; Fails

AllegroGraph check existing triple

戏子无情 提交于 2020-01-04 07:58:32
问题 I am using AllegroGraph 4. I have a triple store, and I am trying to add new triples only if they don't already exist. Here is my Prolog query: (select (?news) (alfas ?news) (a-- ?news !tst:has-annotation !tst:Test))) where alfas checks for a condition(it works fine) and a-- is defined like this: (<-- (a-- ?s ?p ?o) ;; Fails unless all parts ground. (lisp (not (triple-exists-p ?s ?p ?o))) (lisp (add-triple ?s ?p ?o))) I have also tried defining it like this: (<-- (a-- ?s ?p ?o) ;; Fails