Reputation: 48
So the problem given is as follows
Write a program printfigure() to print out the corresponding part of the following figure for input capital character given by a user. Test your program.
For example, when an input is C the print out will be:
Input a capital character: C The corresponding figure is as the follows: A BB CCC
Anyways, I feel like I have mostly figured this out but the code keeps printing past the given letter. I think it's a problem with my recursion and the base case being skipped. Here is my code so far:
alphabet(['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']).
printfigure :- write('Input a capitol character: '),get0(L),
write('The corresponding figure is as follows: '),nl,
alphabet(A),member(L,A,0).
member(X,[X|_],S) :- X =:= X, C1 is S+1, abc(X,C1).
member(X,[H|T],S) :- C1 is S+1, abc(H,C1), member(X,T,C1).
abc(_,0) :- nl.
abc(Q,N) :- write(Q), N1 is N-1, abc(Q,N1).
Any help is great, i'm just hoping to understand what is going wrong.
Upvotes: 1
Views: 121
Reputation: 60004
A way to debug (in any language) it's to separate functionally independent parts.
printfigure :- write('Input a capitol character: '),get0(L),
write('The corresponding figure is as follows: '),nl,
printfigure(L).
printfigure(L) :-
alphabet(A),member(L,A,0).
and test with the input that (I presume) you think will be fed to your code by get0/1
1 ?- printfigure('C').
A
BB
ERROR: =:=/2: Arithmetic: `'C'/0' is not a function
Exception: (9) so:member('C', ['C', 'D', 'E', 'F', 'G', 'H', 'I'|...], 2) ? aabort
you can see that
alphabet([0'A,0'B,0'C...]).
or use a different input predicate, like read/1, or convert the character code to an atom (with atom_codes(A, [C]))then correct the other error...
Upvotes: 1