prolog

Read a file line by line in Prolog

为君一笑 提交于 2019-12-17 06:48:06
问题 I'd like to read a plain text file and apply a predicate to each line (the predicates contain write which does the output). How would I do that? 回答1: In SWI-Prolog, the cleanest solution is to write a DCG that describes what a "line" is, then call a predicate for each line. Use library(pio) to apply the DCG to a file. EDIT : As requested, consider: :- use_module(library(pio)). lines([]) --> call(eos), !. lines([Line|Lines]) --> line(Line), lines(Lines). eos([], []). line([]) --> ( "\n" ; call

Safer type tests in Prolog

北城以北 提交于 2019-12-17 06:15:35
问题 ISO-Prolog (ISO/IEC 13211-1:1995 including Cor.1:2007, Cor.2:2012) offers the following built-in predicates for testing the type of a term: 8.3 Type testing 1 var/1. 2 atom/1. 3 integer/1. 4 float/1. 5 atomic/1. 6 compound/1. 7 nonvar/1. 8 number/1. 9 callable/1. 10 ground/1. 11 acyclic_term/1. Within this group there are those whose purpose is solely to test for a certain instantiation, that is 8.3.1 var/1 , 8.3.7 nonvar/1 , 8.3.10 ground/1 , and those that assume that a term is sufficiently

Integrating Prolog with C# [closed]

依然范特西╮ 提交于 2019-12-17 05:01:51
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Does anyone know of a nice (and preferably free) way to integrate Prolog and C#? Im looking to create a Prolog dll or similar to call from my managed code, and retrieve an answer once all the processing has been complete. Im looking for it to be predominantly one sided (c# calls Prolog). I have seen this

Remove duplicates in list (Prolog)

微笑、不失礼 提交于 2019-12-17 04:34:07
问题 I am completely new to Prolog and trying some exercises. One of them is: Write a predicate set(InList,OutList) which takes as input an arbitrary list, and returns a list in which each element of the input list appears only once. Here is my solution: member(X,[X|_]). member(X,[_|T]) :- member(X,T). set([],[]). set([H|T],[H|Out]) :- not(member(H,T)), set(T,Out). set([H|T],Out) :- member(H,T), set(T,Out). I'm not allowed to use any of built-in predicates (It would be better even do not use not/1

Find powers of 2 in a list Prolog

浪尽此生 提交于 2019-12-17 02:32:28
问题 I'm trying to create a list in Prolog (SWI Prolog) and check which numbers are powers of 2 and second find how many times a specific number is in the list (in this example I'm trying to find how many times the number 3 is in the list). For a example, if you ask ?- check([0,2,3,-5,-2,1,8,7,4], MULT2, THREE). you should see MULT2=[2,8,4] THREE=1 My first try to find a solution is to search the list with head and doing head mod 2 = 0 to find all numbers which are powers of 2, but something went

Prolog: a person is a sibling of himself?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 02:32:07
问题 I'm having some trouble understanding why my code in prolog does something based on the order I put my rules in. Here is my database: parent(tom, bob). parent(tom, liz). parent(mary, bob). parent(mary, liz). male(tom). male(bob). female(mary). female(liz). And here are the rules: %difference(X, Y) ==> Predicate to check if two people X and Y are not the same person. difference(X, Y) :- \==(X, Y). father(X, Y) :- male(X), parent(X, Y), difference(X, Y). mother(X, Y) :- female(X), parent(X, Y),

Prolog: Filtering a list?

我与影子孤独终老i 提交于 2019-12-17 02:24:33
问题 I'm currently working on a very short project on Prolog, and just got stuck trying to apply a "filter" I have created to a list. I have what you could call the filter ready, but I can't apply it. It'd be better if I illustrate: filter(A, B) ...outputs 'true' if certain conditions are met. filterList(A, [X, Y, Z]) ...outputs a list which includes all elements from the second argument that make the filter output false . (So if filter(A, X) is true, the output is [Y, Z] ). I have the "filter"

Prolog - count repetitions in list

霸气de小男生 提交于 2019-12-17 02:00:07
问题 I'm trying to look through a list and count the number of times a given word appears. I've got this so far: count_repetitions([_], [], 0). count_repetitions([Word], [Word|Tail], Count):- count_repetitions([Word], Tail, X), Count is X + 1. count_repetitions([Word], [Z|Tail], Count):- Word \= Z, count_repetitions([Word], Tail, Count). So the query ?- count_repetitions([yes],[yes,and,yes,and,no], X). would give X = 2 . This appears to work. Now I need to write a predicate that outputs a list

Goldbach’s Conjecture in prolog

Deadly 提交于 2019-12-14 03:34:29
问题 Goldbach’s Conjecture : Every positive even number greater than 2 is the sum of two prime numbers. Eg 28 (5,23 and 11,17) I want Prolog code to print below (all combinations) : ?- goldbach(28, L). Output : L = [5,23]; L = [11, 17]; I have a code which prints single combination[5,23], but not the next [11,17]. is_prime(2). is_prime(3). is_prime(P) :- integer(P), P > 3, P mod 2 =\= 0, \+ has_factor(P,3). has_factor(N,L) :- N mod L =:= 0. has_factor(N,L) :- L * L < N, L2 is L + 2, has_factor(N

PROLOG Comparing 2 lists

别说谁变了你拦得住时间么 提交于 2019-12-14 03:24:24
问题 I've been trying for a long time and searched a lot (...) but nothing. What I want is to create a function that will compare elements of one list to the elements of other list. The things is I have absolutely no idea how I'm going to iterate through the members of both lists. Ex. comparing the first member of the first list L1 to the members of L2. If it meets a certain condition (the condition may vary, but compare/4, member and such won't do), I'll pick the second member of L1 to all the