Solving for Multiple variables

24 views
Skip to first unread message

James

unread,
Apr 18, 2016, 4:57:08 PM4/18/16
to swi-p...@googlegroups.com
Hello Everyone,
I have a question about how I can represent one or more variables in a query.

It is best to ask the question with an example:

If I were to write a function that checks whether all the numbers in a list are even and I enter:
checkeven([2,4,6,X]).

I would expect X=8.
Or if I enter:
checkeven([2,4,6,X,Y]).

I would expect X=8, Y=10

How would I write a function in which X can represent one or more variables.
For instance, if I added a constraint in checkeven limiting a list to a particular size:

checkeven([2,4,6,[X]]).

Returning:

X=[8]
X=[8,10]
X=[8,10,12]
X=[8,10,12,14]

and so forth.... up to the maximum size I defined.

Thank you very much for your help

best wishes
James











Barb Knox

unread,
May 12, 2016, 6:08:57 AM5/12/16
to SWI-Prolog, James
On 19 Apr 2016, at 08:57, James <con...@austinkershaw.com> wrote:

Hello Everyone,
I have a question about how I can represent one or more variables in a query.

It is best to ask the question with an example:

If I were to write a function that checks whether all the numbers in a list are even and I enter:
checkeven([2,4,6,X]).

I would expect X=8.

Why?  Why not X=2, or X=0?


Or if I enter:
checkeven([2,4,6,X,Y]).

I would expect X=8, Y=10

Again why?  If the only spec is that the list be only even numbers, they could be any of them.  It looks like what you really want is the list to be some prefix of 2,4,6,8,10,....

How would I write a function in which X can represent one or more variables.
For instance, if I added a constraint in checkeven limiting a list to a particular size:

checkeven([2,4,6,[X]]).

They looks like a kluge.  How about:
length(X, 5), checkeven(X).
which will give you X=[2,4,5,8,10].

And a plain checkeven(X) can give you
X=[];
x=[2];
X=[2,4];
X=[2,3,6};
....

If you want a maximum size you can add that as another arg to checkeven.


Returning:

X=[8]
X=[8,10]
X=[8,10,12]
X=[8,10,12,14]

and so forth.... up to the maximum size I defined.

Thank you very much for your help

best wishes
James


-- 
---------------------------
|  BBB                b    \    Barbara at LivingHistory stop co stop uk
|  B  B   aa     rrr  b     |
|  BBB   a  a   r     bbb   |   ,008015L080180,022036,029037
|  B  B  a  a   r     b  b  |   ,047045,L014114L4.
|  BBB    aa a  r     bbb   |
-----------------------------


Reply all
Reply to author
Forward
0 new messages