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

时光怂恿深爱的人放手 提交于 2019-12-17 16:47:26

问题


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!


回答1:


?- 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...




回答2:


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.




回答3:


Just to put the comment by @mat in an answer:

?- string_codes("string_to_list/2 is deprecated; use string_codes/2!", Codes)
   ;
   false.
Codes = [115, 116, 114, 105, 110, 103, 95, 116, 111|...] /* press w */ [write]
Codes = [115, 116, 114, 105, 110, 103, 95, 116, 111, 95, 108, 105, 115, 116, 47, 50, 32, 105, 115, 32, 100, 101, 112, 114, 101, 99, 97, 116, 101, 100, 59, 32, 117, 115, 101, 32, 115, 116, 114, 105, 110, 103, 95, 99, 111, 100, 101, 115, 47, 50, 33] /* press enter */.

However, from here on all terms will be shown completely. This might get annoying.

What you can also do is just use one of the printing predicates:

?- ..., writeln(Codes).

but this is frowned upon for some reason. It is definitely useful if you have several bindings reported in an answer, but you only want to look at the full value of one of the variables:

?- numlist(1,1000,L),
   Codes = `This is a code list in SWI-Prolog V7`.
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...],
Codes = [84, 104, 105, 115, 32, 105, 115, 32, 97|...].

?- numlist(1,1000,L),
   Codes = `This is a code list in SWI-Prolog V7`,
   writeln(Codes).
[84,104,105,115,32,105,115,32,97,32,99,111,100,101,32,108,105,115,116,32,105,110,32,83,87,73,45,80,114,111,108,111,103,32,86,55]
L = [1, 2, 3, 4, 5, 6, 7, 8, 9|...],
Codes = [84, 104, 105, 115, 32, 105, 115, 32, 97|...].



回答4:


You can solve your question with this,

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

However, if you plan to always show all results and not being shortlisted, then you can add this line in the "init".

Navigation: SWI-Prolog >Settings > User init File

You will be asked to create a swipl.ini file if you have never created one.

At the end of the day, you will not need to key in the above line of code when you start prolog again in the future.




回答5:


I've found two ways.


1.

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

Then do your command that is printing a truncated list.

(set_prolog_flag documentation)


2.

?- atom_chars(goodbye_prolog, X) ; true.

(AllOutput documentation)

Put ; true. at the end of the call that results in a long list. Then push the w key on your keyboard. The result is:

?- sudoku([_,_,2,3,_,_,_,_,_,_,_,_,3,4,_,_], Solution); true.
Solution = [4, 1, 2, 3, 2, 3, 4, 1, 1|...] [write]
Solution = [4, 1, 2, 3, 2, 3, 4, 1, 1, 2, 3, 4, 3, 4, 1, 2] ;
true.


来源:https://stackoverflow.com/questions/32910541/swi-prolog-how-to-show-entire-answer-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!