0

I have a list like

['_', '_', '#', '_']

Is there any way I can let system generate some different variables and replace each '_' with one of the variables? Thanks

jacobbb
  • 101
  • 9
  • Yes. But can you specify how you want your predicate to be called? – lurker Oct 22 '14 at 19:58
  • Someone asked exactly this question a few days ago but I don't see it in the recent history, so probably this is for the same class and your peer deleted the question after it was answered. – Daniel Lyons Oct 22 '14 at 20:00
  • @lurker hmm.. like convert([T|Ts], [A|As]) :- (T == '_' -> .... ; ...). Do you mean this? – jacobbb Oct 22 '14 at 20:04
  • @DanielLyons Ah.. Really? I was trying to solve it by myself, but I tried a lot and searched a lot, still not success... – jacobbb Oct 22 '14 at 20:09
  • A lot of students try that in the Prolog tag. When it happens to me, I usually arrange for a moderator to undelete the question, but if I'm not an answerer I usually don't remember enough to reconstruct the deleted question's URL. Perhaps others with more authority than I have can. – Daniel Lyons Oct 22 '14 at 20:11
  • 2
    possible duplicate of [Why does my replace/2 program always return false?](http://stackoverflow.com/questions/26459971/why-does-my-replace-2-program-always-return-false) – lurker Oct 22 '14 at 20:13
  • @jacobbb not exactly. That's part of a declaration. I was looking for how you wanted to run the query. – lurker Oct 22 '14 at 20:15
  • @lurker That seems a same question with mine, Thanks – jacobbb Oct 22 '14 at 20:21
  • And now you have more answers to pick from. :) – lurker Oct 22 '14 at 20:22

1 Answers1

2

Simple implementation:

var('_', _):- !.
var(X, X).

Example run:

?- maplist(var, ['_','_','#','_'], L).
L = [_G313, _G316, #, _G322].

The cut (exclamation mark) is used to leave out any choicepoints. (Try leaving the cut out, you will see that you will get an idle choicepoint by pressing semicolon.)

Wouter Beek
  • 3,307
  • 16
  • 29