Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWI-Prolog how to show entire answer (list)?

I'm trying to convert a string to a list of ascii-codes like so:

7 ?- string_to_list("I'm a big blue banana in space!", C).
C = [73, 39, 109, 32, 97, 32, 98, 105, 103|...].

8 ?- 

This doesn't give me the entire list as you can see, but I need it.

This solution does not work: I can't press w since it gives me the answer and does a full stop. Neither does this: I can call the function alright, and it returns true, but the list still isn't fully displayed.

11 ?- set_prolog_flag(toplevel_print_options,[quoted(true), portray(true), max_depth(0), spacing(next_argument)]).
true.

12 ?- string_to_list("I'm a big blue banana in space!", C).
C = [73, 39, 109, 32, 97, 32, 98, 105, 103|...].

13 ?- 

Any help appreciated!

like image 487
Mossmyr Avatar asked Oct 02 '15 15:10

Mossmyr


2 Answers

?- set_prolog_flag(answer_write_options,[max_depth(0)]).
true.

?- string_to_list("I'm a big blue banana in space!", C).
C = [73,39,109,32,97,32,98,105,103,32,98,108,117,101,32,98,97,110,97,110,97,32,105,110,32,115,112,97,99,101,33].

there should be somewhere here on SO the same answer... I'm using the last SWI-prolog release, just compiled...

like image 74
CapelliC Avatar answered Oct 14 '22 03:10

CapelliC


If you are finding yourself using string_codes/2 or atom_codes/2 a lot, reconsider your approach. You can use chars in place of codes and avoid the SWI-specific string datatype altogether. All this, by setting a Prolog flag:

?- set_prolog_flag(double_quotes, chars).
true.

?- Chs = "Codes are unreadable!".
Chs = ['C', o, d, e, s, ' ', a, r, e|...].

This is more readable than [67, 111, 100, 101, 115, 32, 97, 114, 101|...], but it still does not solve your problem. However, you can now display such answers compactly using library(double_quotes).

?- use_module(double_quotes).
true.

?- Chs = "Codes are unreadable!".
Chs = "Codes are unreadable!".

?- Chs = "Codes are unreadable", Chs = [C|Chs2].
Chs = "Codes are unreadable",
C = 'C',
Chs2 = "odes are unreadable".

With this setting you can process text much more conveniently. It also fits nicely with DCGs.

like image 29
false Avatar answered Oct 14 '22 02:10

false