prolog

valid bracket list in prolog

元气小坏坏 提交于 2021-01-27 04:06:51
问题 I'm trying to test if a list of brackets is valid. My code: checkbrackets([]). checkbrackets(['('|T]):- T = [')'|List], checkbrackets(List). checkbrackets(['('|T]):- T = ['('|List], append(Rest,[')'],T), checkbrackets(Rest). My code works for ['(', '(', ')', '(', '(', ')', ')', ')'] but it fails for ['(', '(', ')', ')', '(', ')'] . What am I doing wrong? Is it possible to write such a test without additional arguments like counters? 回答1: For the sake of completeness, here is a solution

valid bracket list in prolog

孤街浪徒 提交于 2021-01-27 04:06:14
问题 I'm trying to test if a list of brackets is valid. My code: checkbrackets([]). checkbrackets(['('|T]):- T = [')'|List], checkbrackets(List). checkbrackets(['('|T]):- T = ['('|List], append(Rest,[')'],T), checkbrackets(Rest). My code works for ['(', '(', ')', '(', '(', ')', ')', ')'] but it fails for ['(', '(', ')', ')', '(', ')'] . What am I doing wrong? Is it possible to write such a test without additional arguments like counters? 回答1: For the sake of completeness, here is a solution

Import csv file data to populate a Prolog knowledge base

这一生的挚爱 提交于 2021-01-22 08:50:33
问题 I have a csv file example.csv which contains two columns with header var1 and var2. I want to populate an initially empty Prolog knowledge base file import.pl with repeated facts, while each row of example.csv is treated same: fact(A1, A2). fact(B1, B2). fact(C1, C2). How can I code this in SWI-Prolog ? EDIT, based on answer from @Shevliaskovic : :- use_module(library(csv)). import:- csv_read_file('example.csv', Data, [functor(fact), separator(0';)]), maplist(assert, Data). When import. is

Import csv file data to populate a Prolog knowledge base

纵饮孤独 提交于 2021-01-22 08:49:09
问题 I have a csv file example.csv which contains two columns with header var1 and var2. I want to populate an initially empty Prolog knowledge base file import.pl with repeated facts, while each row of example.csv is treated same: fact(A1, A2). fact(B1, B2). fact(C1, C2). How can I code this in SWI-Prolog ? EDIT, based on answer from @Shevliaskovic : :- use_module(library(csv)). import:- csv_read_file('example.csv', Data, [functor(fact), separator(0';)]), maplist(assert, Data). When import. is

Import csv file data to populate a Prolog knowledge base

元气小坏坏 提交于 2021-01-22 08:48:45
问题 I have a csv file example.csv which contains two columns with header var1 and var2. I want to populate an initially empty Prolog knowledge base file import.pl with repeated facts, while each row of example.csv is treated same: fact(A1, A2). fact(B1, B2). fact(C1, C2). How can I code this in SWI-Prolog ? EDIT, based on answer from @Shevliaskovic : :- use_module(library(csv)). import:- csv_read_file('example.csv', Data, [functor(fact), separator(0';)]), maplist(assert, Data). When import. is

Unification with STO detection

扶醉桌前 提交于 2021-01-20 18:52:47
问题 In ISO Prolog unification is defined only for those cases that are NSTO (not subject to occurs-check). The idea behind is to cover those cases of unifications that are mostly used in programs and that are actually supported by all Prolog systems. More specifically, ISO/IEC 13211-1:1995 reads: 7.3.3 Subject to occurs-check (STO) and not subject to occurs-check (NSTO) A set of equations (or two terms) is "subject to occurs- check" (STO) iff there exists a way to proceed through the steps of the

Unification with STO detection

拥有回忆 提交于 2021-01-20 18:50:00
问题 In ISO Prolog unification is defined only for those cases that are NSTO (not subject to occurs-check). The idea behind is to cover those cases of unifications that are mostly used in programs and that are actually supported by all Prolog systems. More specifically, ISO/IEC 13211-1:1995 reads: 7.3.3 Subject to occurs-check (STO) and not subject to occurs-check (NSTO) A set of equations (or two terms) is "subject to occurs- check" (STO) iff there exists a way to proceed through the steps of the

Unification with STO detection

本小妞迷上赌 提交于 2021-01-20 18:46:41
问题 In ISO Prolog unification is defined only for those cases that are NSTO (not subject to occurs-check). The idea behind is to cover those cases of unifications that are mostly used in programs and that are actually supported by all Prolog systems. More specifically, ISO/IEC 13211-1:1995 reads: 7.3.3 Subject to occurs-check (STO) and not subject to occurs-check (NSTO) A set of equations (or two terms) is "subject to occurs- check" (STO) iff there exists a way to proceed through the steps of the

Proper unify_with_occurs_check/2 in SWI-Prolog?

余生颓废 提交于 2021-01-16 01:13:52
问题 Got this strange behaviour. I was running these test cases: s1 :- Q=[[lambda,symbol(_3026),[cons,[quote,_3434], [quote,_3514]]],[quote,_3206]], P=[_3434|_3514], freeze(_3434, (write(foo), nl)), unify_with_occurs_check(P, Q). s2 :- Q=[[lambda,symbol(_3026),[cons,[quote,_3434], [quote,_3514]]],[quote,_3206]], P=[_3434|_3514], freeze(_3434, (write(foo), nl)), freeze(_3514, (write(bar), nl)), unify_with_occurs_check(P, Q). Now I get these results, where the outcome of s2 is wrong. The outcome is

Proper unify_with_occurs_check/2 in SWI-Prolog?

北慕城南 提交于 2021-01-16 01:09:28
问题 Got this strange behaviour. I was running these test cases: s1 :- Q=[[lambda,symbol(_3026),[cons,[quote,_3434], [quote,_3514]]],[quote,_3206]], P=[_3434|_3514], freeze(_3434, (write(foo), nl)), unify_with_occurs_check(P, Q). s2 :- Q=[[lambda,symbol(_3026),[cons,[quote,_3434], [quote,_3514]]],[quote,_3206]], P=[_3434|_3514], freeze(_3434, (write(foo), nl)), freeze(_3514, (write(bar), nl)), unify_with_occurs_check(P, Q). Now I get these results, where the outcome of s2 is wrong. The outcome is