For Programmers: Free Programming Magazines  


Home > Archive > Prolog > June 2005 > What's wrong??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author What's wrong??
martin

2005-06-09, 8:57 am

Hello,

I have something like that:

chkr([X|R]):-
M is 1,chkr(X,R,M).
chkr(X,[Y|R],A):-
A<5,X\=Y,B is A+1,X>=1,X=<2,Y>=1,Y=<2,chkr(Y,R,B).
chkr(X,[],A).

I want it to generate a list of integers. The list should have 5 elements
and all of them should be 1 or 2. When I am trying to execute it I get no
list. Where is a bug?
Thanks for help

regards,
martin


Nick Wedd

2005-06-09, 8:57 am

In message <d851eb$m42$1@julia.coi.pw.edu.pl>, martin <tryf@wp.pl>
writes
>Hello,
>
>I have something like that:
>
>chkr([X|R]):-
> M is 1,chkr(X,R,M).
>chkr(X,[Y|R],A):-
> A<5,X\=Y,B is A+1,X>=1,X=<2,Y>=1,Y=<2,chkr(Y,R,B).
>chkr(X,[],A).
>
>I want it to generate a list of integers. The list should have 5 elements
>and all of them should be 1 or 2. When I am trying to execute it I get no
>list. Where is a bug?
>Thanks for help


>chkr([X|R]):-
> M is 1,chkr(X,R,M).

This clause is ok, but it is inefficient. If you know that M is 1, why
not just type 1:
chkr([X|R]):-
chkr(X,R,1).

Anyway - I deduce from this that you will be calling chkr/3 with its
third argument instantiated to an integer, and its first two arguments
not instantiated.

chkr(X,[Y|R],A):-
A<5,
X\=Y,
B is A+1,
X>=1,
X=<2,
Y>=1,
Y=<2,
chkr(Y,R,B).

When you call this, X is uninstantiated. So what are
X\=Y, and X>=1, and X=<2
going to do? I have no idea, I expect they will produce error messages.
Anyway, I am sure they won't do anything useful. If you are expecting
Prolog to suggest for itself something which is not less than 1, and not
more than 2, and is an integer (not that you have specified this), then
you are optimistic.

Also, each time you try to extend the list, you are checking the value
of the new element twice - this is inefficient.

Here is a hint - use a predicate like
isoneortwo( 1 ).
isoneortwo( 2 ).

Nick
--
Nick Wedd nick@maproom.co.uk
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com