rnso
rnso

Reputation: 24535

Prolog executable writing String in codes

I am trying to create an executable of following Prolog code:

item(one, 50, 40).
item(two, 80, 70).
item(three, 100, 55).
item(four, 50, 45).

maxMeanStudy:-  
    findall(M,item(_,M,_),L),
    max_member(Max,L),
    write("Maximum mean value:"),   % this line is not printed properly.
    writeln(Max),!.

main:-
    maxMeanStudy.

I am using following command to create executable as mentioned on this page: http://www.swi-prolog.org/pldoc/man?section=cmdlinecomp

$ swipl --goal=main --stand_alone=true -o myprog -c myques.pl

The executable created does not write the string "Maximum mean value:" in letters but writes it in codes:

$ ./myprog
[77,97,120,105,109,117,109,32,109,101,97,110,32,118,97,108,117,101,58]100

I am working on Linux (32bit). How can I solve this problem. Thanks for your help.

Upvotes: 1

Views: 386

Answers (1)

mat
mat

Reputation: 40768

In almost all such cases, it turns out that:

  1. You should not use side-effects in the first place. Instead, define relations that you can actually reason about. In your case, you are describing a relation between mean values and their maximum. Therefore, the name maximum_mean_value(Ms, M) suggests itself. And is_that_not_more_readable than mixingLowerAndUpperCaseLetters?

  2. Let the toplevel do the printing for you, via pure queries like:

    ?- maximum_mean_value(Ms, M).
    M = ... . % result is automatically shown by the toplevel!
    
  3. If you really need to write something on the terminal, do it in a separate predicate. Avoid mixing pure and impure code.

  4. Use format/2 for formatting output. For example:

    maximum_mean_value(Ms, M),
    format("Maximum mean value: ~d\n", [M]) 
    

    Note how format/2 makes it easy to output text that involves other terms.

Upvotes: 3

Related Questions