Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog code gives two different results

Tags:

list

prolog

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!

like image 444
Jataki Avatar asked Aug 16 '26 00:08

Jataki


1 Answers

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 portability!

like image 92
repeat Avatar answered Aug 18 '26 18:08

repeat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!