Prolog code gives two different results
So, I am doing this code where there is a function that takes 2 arguments and reports that one of them is not a list.
The code is as follows:
/*** List Check ***/
islist(L) :- L == [], !.
islist(L) :- nonvar(L), aux_list(L).
aux_list([_|_]).
/*** Double List Check ***/
double_check(L, L1) :- \+islist(L) -> write("List 1 invalid");
\+islist(L1)-> write("List 2`invalid"); write("Success").
This will work. The online code does exactly what I want. But on the Prolog console of my computer, it gives a completely different answer:
?- double_check(a, [a]).
[76,105,115,116,97,32,49,32,105,110,118,97,108,105,100,97]
true.
Example. I have no IDEA where this list comes from. Can someone tell me my mistake and help me fix this please? Thanks everyone!
source to share
Quick fix: use format/2
instead write/1
! For more information on inline predicate format/2
, click here .
$ swipl --traditional
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.37) [...]
?- write("abc").
[97,98,99] % output by write/1 via side-effect
true. % truth value of query (success)
?- format('~s',["abc"]).
abc % output by format/2 via side-effect
true. % truth value (success)
However with different command line arguments:
$ swipl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.37) [...]
?- write("abc").
abc
true.
?- format('~s',["abc"]).
abc
true.
While this may sound frustrating, I recommend using the command line option --traditional
for SWI-Prolog in combination with format/2
instead write/1
. Saveportability!
source to share