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

problem with anonymous variables
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.


Report this thread to moderator Post Follow-up to this message
Old Post
ouyang.jie@gmail.com
05-28-05 01:57 AM


Re: problem with anonymous variables
ouyang.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

Report this thread to moderator Post Follow-up to this message
Old Post
Bart Demoen
05-28-05 01:57 AM


Re: problem with anonymous variables
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?


Report this thread to moderator Post Follow-up to this message
Old Post
ouyang.jie@gmail.com
05-28-05 01:57 AM


Re: problem with anonymous variables

ouyang.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.


Report this thread to moderator Post Follow-up to this message
Old Post
Brian Hulley
05-28-05 01:57 AM


Re: problem with anonymous variables
A 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.
>



Report this thread to moderator Post Follow-up to this message
Old Post
Anders Lindén
05-29-05 08:59 AM


Re: problem with anonymous variables
"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.

Report this thread to moderator Post Follow-up to this message
Old Post
Torkel Franzen
05-29-05 08:59 AM


Re: problem with anonymous variables
yeah, 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.



Report this thread to moderator Post Follow-up to this message
Old Post
Anders Lindén
05-29-05 08:57 PM


Re: problem with anonymous variables
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).


| ?-  shrink([4,5,B,d(r,e)],C).
C = [4,5,d(r,e)] ? ;
no




Report this thread to moderator Post Follow-up to this message
Old Post
Anders Lindén
05-29-05 08:57 PM


Re: problem with anonymous variables
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

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


Report this thread to moderator Post Follow-up to this message
Old Post
ouyang.jie@gmail.com
05-31-05 09:01 PM


Re: problem with anonymous variables
<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



Report this thread to moderator Post Follow-up to this message
Old Post
John Fletcher
06-01-05 08:57 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
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 06:36 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.