Code Comments
Programming Forum and web based access to our favorite programming groups.hi, i have a problem with counting non-anonymous variables in a list of variables. the straitforward way i come up with is to remove all anonymous variables from the list and then count the length of the resulted list. Anybody has a better idea or how to remove anonymous variables? thx in advance.
Post Follow-up to this messageouyang.jie@gmail.com wrote: > hi, i have a problem with counting non-anonymous variables in a list of > variables. What is an "anonymous" variable in a list ? Is it a variable that occurs only once in the list ? Cheers Bart Demoen
Post Follow-up to this messagean anonymous variable is representated by an underscore "_". for instance , f(_,A,_) has 2 anonymous variables. given a list [_,A,B,_], i want to have the list [A,B] returned or 2 as the number of non-anonymous variables returned. any idea?
Post Follow-up to this messageouyang.jie@gmail.com wrote: > an anonymous variable is representated by an underscore "_". for > instance , f(_,A,_) has 2 anonymous variables. given a list [_,A,B,_], > i want to have the list [A,B] returned or 2 as the number of > non-anonymous variables returned. any idea? As far as Prolog terms are concerned there is no difference between [_,A,B,_] and [X,Y,Z,W] since in all cases you have 4 variables all distinct from each other. Ie the anonymous variable is a convenience only (so you don't have to think up names for variables you don't care about and so that you can get the singleton-variable warning when a non-anonymous variable is only used once), and although implementations can make use of the fact that an anonymous variable has been used to improve performance, the language itself has no way to distinguish an anonymous variable from any other variable.
Post Follow-up to this messageA list doesnt contain variables, only constants and functors. When you unify a list with a predicate term, that predicate can refer to tho se constants and functors in an anonymous way, and _that_ is by the use of vari ables. if you do this query: pred([1,2,3],c) that will unify with the predicate pred([A|B],c) :- foo(A) so that foo(1) will remain in the resolvent. Note that the variables A and B are only having the life length of the unification process. The same query would also match this predicate: pred([_|_],c). But here we see that the predicate doesnt save the unification results in va riables like A and B. The unification will result in a fact, because we doesnt demand that a foo/1 predicate must be true as in the example before. /Anders <ouyang.jie@gmail.com> wrote in message news:1117218983.597268.155000@g44g2000cwa.googlegro ups.com... > hi, i have a problem with counting non-anonymous variables in a list of > variables. the straitforward way i come up with is to remove all > anonymous variables from the list and then count the length of the > resulted list. Anybody has a better idea or how to remove anonymous > variables? thx in advance. >
Post Follow-up to this message"Anders Lindén" <xxxx@xxx.xx> writes: > A list doesnt contain variables, only constants and functors. The Herbrand universe doesn't contain variables, but lists, like other Prolog structures, can be incomplete and contain unbound variables.
Post Follow-up to this messageyeah, thats actually right, I understood what he meant after I had replied to his posting :) Sorry! /Anders "Torkel Franzen" <torkel@sm.luth.se> wrote in message news:vcbmzqe52zx.fsf@beta19.sm.ltu.se .. > "Anders Lindén" <xxxx@xxx.xx> writes: > > > The Herbrand universe doesn't contain variables, but lists, like > other Prolog structures, can be incomplete and contain unbound > variables.
Post Follow-up to this messageOk, here is an attempt shrink([],[]). shrink([A|B],C):- var(A),!, shrink(B,C). shrink([A|B],[A|C]):- nonvar(A), shrink(B,C). | ?- shrink([4,5,B,d(r,e)],C). C = [4,5,d(r,e)] ? ; no
Post Follow-up to this messagethank you for all your guys' reply. actually i wanted to distinguish a non-anonymous variable, eg. A,B,..., from an anonymous variable,eg, _. However i realized that it is unable to make it according to how prolog works. i am trying to walk around this problem Anders Lind=E9n wrote: > Ok, here is an attempt > > shrink([],[]). > shrink([A|B],C):- var(A),!, shrink(B,C). > shrink([A|B],[A|C]):- nonvar(A), shrink(B,C). >=20 >=20 > | ?- shrink([4,5,B,d(r,e)],C). > C =3D [4,5,d(r,e)] ? ; > no
Post Follow-up to this message<ouyang.jie@gmail.com> wrote in message news:1117555181.352717.206360@g49g2000cwa.googlegroups.com... thank you for all your guys' reply. actually i wanted to distinguish a non-anonymous variable, eg. A,B,..., from an anonymous variable,eg, _. However i realized that it is unable to make it according to how prolog works. i am trying to walk around this problem <snipped> Prolog can distinguish anonymous variables when reading terms. Look up read_term/[2,3] in your manual, specifically the 'variable_names' option. Rgds
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.