Code Comments
Programming Forum and web based access to our favorite programming groups.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 ---------------------------------------------------
Post Follow-up to this message"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.
Post Follow-up to this messageKonrad 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
Post Follow-up to this messageOn 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.