0
votes

In prolog, is there any way to make a variable an instance of _? I'm making a sudoku solver, and I represent the puzzle read in by either numbers (given numbers) or -'s, (numbers not given). So, I read the whole puzzle into a list of lists, and now I want to pass that list to a function, and I need for '-' to be passed as _. Is there some way when I'm reading input to store the input as a _? Like for example..

get_next(X) :-
  repeat,
  get_char(Y),
  (Y = '\n' -> fail
   ;
   Y = '-' -> X = _
   ;
   X = Y
  ).

Something like this? I thought perhaps passing a '_' would do it, but of course, '_' \= _ ... Any help would be greatly appreciated. Prolog is very foreign to me.

1
An underscore in Prolog is a variable that does not unify with other variables of the same name. That is its only difference from other named variables. - Sergey Kalinichenko
So, If I call all_different([1,_,2,5,_,4,_,8,7]), it is true, but when I use the above function to read the input, and do all_different(Input) it treats Input as [1,_G#,2,5,_G# ... ] and I get the error message ERROR: >/2: Arithmetic: 2/0' is not a function`. What can I do to fix this? Implement my own all_different? - Jay Elrod
What I meant was that all_different([1,_,2,5,_,4,_,8,7]) and all_different([1,A,2,5,B,4,C,8,7]) is the same as long as you do not try unifying A, B, or C in other places in your rule. - Sergey Kalinichenko

1 Answers

0
votes

Just don't bind X with Y when Y='-'. That will leave X uninstantiated (provided it was uninstantiated in the first place):

get_next(X) :-
  repeat,
  get_char(Y),
  (Y = '\n' -> fail
   ;
   Y \= '-' -> X = Y  ; true
  ).