Re: [eclipse-clp-users] possible to turn strings into variable names?

From: Joachim Schimpf <joachim.schimpf_at_...44...>
Date: Thu, 11 Mar 2010 10:51:03 +1100
WL Ko wrote:
> Hi all,
> 
> There exists a testing method that I've seen in some example programs, in
> the form of having test(1, tree(a,nil,nil)) as a fact, and then running
> test(1,X), in_order(X,L).
> 
> I'm trying to apply that to a predicate that takes in variables with
> particular names, like sendmore([S,E,N,D]+[M,O,R,E]=[M,O,N,E,Y]), and for
> that to be possible with variables, it seems that I should store it as a
> string, but I do not know of a way to convert the string back into a term...

No, you don't need strings.

Imagine that variables are really just "holes" or empty spaces
in a data structure, which can be filled with actual values later.

Variable _names_ are not used during computation, you only use them
in a textual representation to indicate which of these empty spaces
are the same, and which are different.

For example, if you write [A,B,3,B,C] you are specifying a list with
5 elements, where the 3rd one is already known to be 3, and the others
are still empty spaces.  However, by using the letter B for the 2nd and
the 4th, you have indicated that both these positions really refer to
the same empty space, and when this space gets filled, the value will
appear in two places in the list.  So the names here are only used as a
way to construct "a list where the 2nd and 4th elements are identical".

You could have made a completely equivalent list (Xs) via any of the
following pieces of code:
?- Xs = [_,Z,3,Z,_].
?- Xs = [A,B,C,D,E], B=D, C=3.
?- length(Xs,5), nth1(2,Xs,X), nth1(4,Xs,X), nth1(3,Xs,3).
?- functor(S,f,5), arg(2,S,X), arg(4,S,X), arg(3,S,3), S=..[_|Xs].

To come back to your question: the sendmore/1 predicate only cares about
the _structure_ of its input, i.e. where the variables are, and which of
them are shared.  So nobody stops you from having facts like
test(1, [S,E,N,D]+[M,O,R,E]=[M,O,N,E,Y]).
and running your test by calling
?- test(1, Equation), sendmore(Equation), writeln(Equation).

Easy, isn't it ;-)

-- Joachim
Received on Wed Mar 10 2010 - 23:51:13 CET

This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:20 CEST