Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined procedure in SWI-Prolog

Tags:

prolog

Ok i got these two predicated hangman and graphics

fail([]).

hangman:-
    getPhrase(Ans), 
    !, 
    write('Welcome to hangman.'),
    nl,
    fail(FailedList), 
    graphics(FailedList), %<--- The call is made here.
    name(Ans,AnsList), 
    makeBlanks(AnsList, BlankList),
    getGuess(AnsList,BlankList, FailedList).


graphics(FailedList):-
    length(FailedList, L),
    L == 0,
    write('-----------'), nl,
    write('|'), nl,
    write('|'), nl,
    write('|'), nl,
    write('|'), nl,
    write('|'), nl,
    write('|'), nl,
    write('|'), nl,
    write('|'), nl,
    write('/\'), nl.

Why do i get the error: ERROR: hangman/0: Undefined procedure: graphics/1?
Note that if i put the predicate graphics inside hangman in comments my program works fine.

like image 363
Kostas Avatar asked Dec 19 '25 05:12

Kostas


2 Answers

(this answer is not really about the question asked so please let the answer to @SeçkinSavaşçı who did a great job answering the question, it's more of a code review)

Here, you visibly want to test if a list is empty and react accordingly by displaying some things. For the test if a list is empty part, you're doing it wrong:

graphics(FailedList):-
    length(FailedList, L),
    L == 0,
    % some IO stuff

In prolog, you can use unification in a more straightforward manner:

graphics(FailedList):-
    length(FailedList, 0),
    % some IO stuff

or, better, where you directly test for the empty list in the head as a condition to execute the body of the predicate:

graphics([]):-
    % some IO stuff

For the IO part, you're doing it kinda wrong again. SWI-Prolog, for example, has a writeln/1 predicate, that would make your code lighter:

graphics([]):-
    writeln('-----------'),
    writeln('|'),
    writeln('|'),
    writeln('|'),
    writeln('|'),
    writeln('|'),
    writeln('|'),
    writeln('|'),
    writeln('|'),
    writeln('/\\').

Still better, the format/1 predicate could be used:

graphics([]):-
    format('-----------~n|~18~n|~n|~n|~n|~n|~n|~n|~n/\\').

The main predicate seems to have some problems too, but I'll let you look into it and ask questions if you're stuck somewhere :)

like image 168
m09 Avatar answered Dec 21 '25 20:12

m09


 write('/\'), nl.

In the last line, you are escaping the ending quotation mark with \'. Change it to:

 write('/\\'), nl.

BTW: @Mog has written the answer before I had a look at the comments, I tested it and now it finds graphics/1.

like image 42
Seçkin Savaşçı Avatar answered Dec 21 '25 21:12

Seçkin Savaşçı



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!