sher
sher

Reputation: 1

prolog printing lists

?-mod5(X,[a,16,b,c(5),[[12]],8]).  
X = [a,1,b,c(5),[[12]],3].  

A non-integer should remain untouched. However, in the case of [[12]], since 12 is the ASCII value of "\f" (form feed), it outputs [["\f"]] when what I want it to output [[12]] as is. How can I prevent this?

Upvotes: 0

Views: 1367

Answers (1)

user206428
user206428

Reputation:

EDIT: At first, I assumed the problem was with the implementation, which is why I've suggested one below. It turns out, given the OP has later informed that this is NU-Prolog, that the answer seems to lie simply in the default behaviour of NU-Prolog, as per this manual, section 3.7:

"Lists are usually printed as strings if every element of the list is an integer for which isPrint/1 is true."

As isPrint/1 is denoted to be a built-in for NU-Prolog, it is possible it may not be overridden to provide other behaviour - perhaps NU-Prolog can be configured to alter the default behaviour in this case, but I'm uncertain.

Below is an implementation of mod5/2, which is now irrelevant to the solution:


If the predicate mod5/2 is intended to take (as a second argument) a list of terms, and return (in the first argument position) a list of terms in the same order, but where every number N is replaced with the value of N mod 5, then I suggest you try:

mod5([], []).
mod5([X|Xs], [Y|Ys]) :-
    isInt(Y), !,
    X is Y mod 5,
    mod5(Xs, Ys).
mod5([X|Xs], [X|Ys]) :-
    mod5(Xs, Ys).

Note the guard subgoals isInt(Y), !, (NU-PROLOG) in the second clause of mod5/2; the numeric calculation of mod via is to replace Y will only occur iff Y is numeric; otherwise, it is passed through untouched by the last clause of mod5 (as X).

Executing this with your example using SWI-PROLOG gives:

?- mod5(X,[a,16,b,c(5),[[12]],8]).  
X = [a, 1, b, c(5), [[12]], 3] ;
false.

Upvotes: 1

Related Questions