简体   繁体   中英

Prolog - an Aux predicate in a main predicate

vecLine2BitLineAux([C | Cs],[P | Ps], N, LastP, LastC, BitLine) :-
    /* print(P), print(' '), print(C), print(' '), print(N),
       print(' '), print(LastP), print(' '), print(LastC),
       print(' '), print(BitLine), print('\n'), */
    vecLine2BitLineAux(Cs,Ps, N, P, C, Res),
    SpCnt is P-(LastP+LastC),
    /* print(SpCnt),print('\n'), */
    fill_char(SpCnt, ' ', Spaces),
    fill_char(LastC, '*', Stars),
    append(Stars, Spaces, Tmp),
    append(Tmp, Res, BitLine).

vecLine2BitLine(CList, [P | Ps], N, BitLine) :-
   P1 is P-1,
   vecLine2BitLineAux(Clist, [P | Ps], N, P1, 0, BitLine).

The main predicate should call the Aux predicate with 2 additional parameters, which are (P-1) and 0, but some why it gives the error:

 Exception: (11) _L191 is 9- (5+_G559) ?

But if I just run the Aux predicate with the next example, it works great:

vecLine2BitLineAux([1,2,1],[2,5,9],12, 1,0,BitLine).

Any one knows why and how I can fix it ?

The error message there is basically telling you that in this expression:

SpCnt is P-(LastP+LastC),

the variable LastC arrives without a binding.

You didn't include enough code for us to really debug, but I notice that when I loaded your code I got this message:

Warning: /Users/dlyons/Desktop/test.pl:11:
Singleton variables: [CList,Clist]

Prolog is extremely case sensitive, and you must regard Singleton variable warnings as critical errors! If you do not, you will wind up with broken code. I think it's likely that if you correct the typo (calling CList Clist ) in the body of vecLine2BitLine/4 the problem will go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM