Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How to find the letters?
I have just started with Prolog and tried following code
letter(C) :- (C >= 0'A , C =< 0'Z).

What i expected the software to do when i call
?- letter(X).
is to find 65, then 66, then 67, then... and then 91 and then
simply no. To my surprise it tells me that there's an error of
instantiating the variable at the first position of ">=".
What did i miss?

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------

Report this thread to moderator Post Follow-up to this message
Old Post
Konrad Den Ende
10-01-04 01:59 PM


Re: How to find the letters?
"Konrad Den Ende" <tmp1@viltersten.com> writes:

> I have just started with Prolog and tried following code
> letter(C) :- (C >= 0'A , C =< 0'Z).
>
> What i expected the software to do when i call
> ?- letter(X).
> is to find 65, then 66, then 67, then... and then 91 and then
> simply no. To my surprise it tells me that there's an error of
> instantiating the variable at the first position of ">=".
> What did i miss?

You can't have free variables in math expressions.  If you've
got R = 5 and you tell Prolog to give you R + 10, it'll just
change R to 5 and send 5 + 10 to the math guy.  The math guy
is into maths *only*.  All the searching & backtracking stuff,
he doesn't want any of it.  Sorry.

Report this thread to moderator Post Follow-up to this message
Old Post
Mikael Brockman
10-01-04 08:57 PM


Re: How to find the letters?
Konrad Den Ende schrieb:
> I have just started with Prolog and tried following code
> letter(C) :- (C >= 0'A , C =< 0'Z).
>
> What i expected the software to do when i call
> ?- letter(X).
> is to find 65, then 66, then 67, then... and then 91 and then
> simply no. To my surprise it tells me that there's an error of
> instantiating the variable at the first position of ">=".
> What did i miss?
>

you have to define your predicate in another way (>=, =<, etc. are
comparing functions that want the two arguments instantiated, so no free
variables are allowed here).

Try to make a predicate between(A, X, B) which is successful for values
X between A and B, so you can define letter/1:
letter(X) :- between(0'A, X, 0'Z).

With an appropriate design of between/3, X can be a free variable.
Something like that counts all values between A and B and tests if X is
one of them (e.g. make the intervall smaller and check if X is a bound
of the intervall).

--
Matthias

Report this thread to moderator Post Follow-up to this message
Old Post
Matthias Kretschmer
10-01-04 08:57 PM


Re: How to find the letters?
On Fri, 1 Oct 2004, Konrad Den Ende wrote:

> I have just started with Prolog and tried following code
> letter(C) :- (C >= 0'A , C =< 0'Z).
>
> What i expected the software to do when i call
> ?- letter(X).
> is to find 65, then 66, then 67, then... and then 91 and then
> simply no. To my surprise it tells me that there's an error of
> instantiating the variable at the first position of ">=".
> What did i miss?

How should it know that C is only expected to be an integer ?

Also, the comparisions require their arguments to be ground expresssions.
(If not, one can get a run-time instantation error, as you noticed.)
(Also, is/2 have similar problems, in this sense.)

However, one can do things like this with Constraint Logic Programming
(CLP), e.g. CLP(FD) :

SICStus 3.11.1 (x86-linux-glibc2.3): Fri Feb 20 18:38:25 CET 2004
| ?- use_module(library(clpfd)).
......
yes
| ?- C #>= 0'A,C #=< 0'Z.
C in 65..90 ? ;
no
| ?- C #>= 0'A,C #=< 0'Z,indomain(C).
C = 65 ? ;
C = 66 ? ;
C = 67 ? ;
C = 68 ? ;
C = 69 ? ;
C = 70 ?
yes
| ?- C #>= 0'A,C #=< 0'Z,indomain(C),put(C),fail.
ABCDEFGHIJKLMNOPQRSTUVWXYZ
no
| ?-

> --
>
> Kindly
> Konrad

--
Stefan Lj
md9slj

The infinity that can be finitely expressed is not the true infinity

Report this thread to moderator Post Follow-up to this message
Old Post
Stefan Ljungstrand
10-01-04 08:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:41 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.