Jataki
Jataki

Reputation: 33

Prolog code gives two different results

So I'm making this code where there's a function that receives 2 arguments and tells if one of them is not a list.

The code is the following:

/*** 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").

It shuold be working. Online the code does exactly what I want it to. 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 that list came from. Can someone tell me my error and help me fix it please? Thank you all!

Upvotes: 3

Views: 248

Answers (1)

repeat
repeat

Reputation: 18726

Quick fix: Use format/2 instead of write/1! For more information on the built-in 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.

Even though it may seem like a nuisance, I recommend using command-line option --traditional for SWI-Prolog, in combination with format/2 instead of write/1. Preserve !

Upvotes: 2

Related Questions