prolog

Interleaving Elements of a Prolog list

不羁岁月 提交于 2020-01-02 22:09:13
问题 I am new to Prolog and came across this practice excercise. The question asks to define a predicate zipper([[List1,List2]], Zippered). //this is two lists within one list. This predicate should interleave elements of List1 with elements of List2. For example, zipper([[1,3,5,7], [2,4,6,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8]. zipper([[1,3,5], [2,4,6,7,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8]. So far I have a solution for two different list: zipper ([],[],Z). zipper([X],[],[X]).

Interleaving Elements of a Prolog list

泄露秘密 提交于 2020-01-02 22:08:31
问题 I am new to Prolog and came across this practice excercise. The question asks to define a predicate zipper([[List1,List2]], Zippered). //this is two lists within one list. This predicate should interleave elements of List1 with elements of List2. For example, zipper([[1,3,5,7], [2,4,6,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8]. zipper([[1,3,5], [2,4,6,7,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8]. So far I have a solution for two different list: zipper ([],[],Z). zipper([X],[],[X]).

Prolog powerset predicate [closed]

牧云@^-^@ 提交于 2020-01-02 19:32:42
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I wish to define a predicate powerset(X, P) which is true when P is the powerset of X. Should work whether or not P is ground. 回答1: Since you use SICStus Prolog you can use the subseq0(+Sequence, ?SubSequence)

Inserting X in its correct position in a sorted list

♀尐吖头ヾ 提交于 2020-01-02 16:23:09
问题 In prolog how do I insert X in its correct position in a sorted list? My Attempt: insert(X,[Y|Rest],[X,Y|Rest]):- X @< Y; insert(X,Rest,BiggerRest). 回答1: You're on the right track, but you need to make this three cases. insert(X, [], [X]). insert(X, [Y|Rest], [X,Y|Rest]) :- X @< Y, !. insert(X, [Y|Rest0], [Y|Rest]) :- insert(X, Rest0, Rest). 来源: https://stackoverflow.com/questions/9004265/inserting-x-in-its-correct-position-in-a-sorted-list

Prolog binding arguments

你离开我真会死。 提交于 2020-01-02 16:19:02
问题 In sicstus prolog, there's a predicate: maplist(:Pred, +List) Pred is supposed to take just one argument - List element. How can I pass a 2-argument predicate, with first argument defined? In other languages it would be written as: maplist(pred.bind(SomeValue), List) 回答1: maplist(P_1, Xs) will call call(P_1, X) for each element of Xs . The built-in predicate call/2 adds one further argument to P_1 and then calls this with call/1 . To indicate that a further argument is needed, it is very

Prolog binding arguments

两盒软妹~` 提交于 2020-01-02 16:18:35
问题 In sicstus prolog, there's a predicate: maplist(:Pred, +List) Pred is supposed to take just one argument - List element. How can I pass a 2-argument predicate, with first argument defined? In other languages it would be written as: maplist(pred.bind(SomeValue), List) 回答1: maplist(P_1, Xs) will call call(P_1, X) for each element of Xs . The built-in predicate call/2 adds one further argument to P_1 and then calls this with call/1 . To indicate that a further argument is needed, it is very

Prolog - Recursive call

落爺英雄遲暮 提交于 2020-01-02 15:54:36
问题 I am having trouble with recursive searching of list and creation of list of lists from the result.. The knowledge base contains team name, number of wins and zone they are in, all associated withe their team number. I am passing list of team numbers in Teams and I am searching for a matching pair with findMinMax/3 . The result I need is... List of lists of paired teams (ex. X = [[gonzaga, washington], [iowa, oklahoma], …] ) And 1 unmatched team (resulted from odd number of teams) or 0 (in

Prolog - Recursive call

本秂侑毒 提交于 2020-01-02 15:53:35
问题 I am having trouble with recursive searching of list and creation of list of lists from the result.. The knowledge base contains team name, number of wins and zone they are in, all associated withe their team number. I am passing list of team numbers in Teams and I am searching for a matching pair with findMinMax/3 . The result I need is... List of lists of paired teams (ex. X = [[gonzaga, washington], [iowa, oklahoma], …] ) And 1 unmatched team (resulted from odd number of teams) or 0 (in

Prolog Constraint Programing finding even and odd numbers

拟墨画扇 提交于 2020-01-02 15:24:06
问题 I need to create a predicate: applyConstraints(L) That applies constraints to the variables in L such that no two contiguous elements in L are both odd or even how can I do that? With a fixed size L it's simple but what about a variable size L? I need that to be done using sicstus-prolog clpfd library. 回答1: Inspired by @MatsCarlsson's version, I tried to minimize the number of constrained variables involved: applyConstraints(Xs) :- S #\= R, applyConstraints(Xs, S, R). applyConstraints([], _,

Representing a system of equations about classes of objects

六眼飞鱼酱① 提交于 2020-01-02 10:07:42
问题 I am looking for suggestions about design patterns for how to describe systems of equations in Prolog, where each equation is a general statement about classes of objects and points in time. Say that i have: A = 0.5 * B + C^2, and B = 5 * D. Given, e.g. D = 1 and C = 12, I want Prolog to compute the value of A. But I want to do this in a case where A, B, C, and D are general classes of objects and time. The code below is working but I find it to wordy, specially the description predicate in