2
\$\begingroup\$

I'm new in Prolog. So, I'm not sure if my code is ok and optimized. I'm also not sure if I use recursion correctly.

Task: find students that has at least 1 bad grade (1 or 2) and print them in table


hasBadGrades(N) :- student(_, N, G), containsBadGrades(G).

containsBadGrades([H|T]) :- wildcard_match('*/[1-2]', H); containsBadGrades(T).

printStudent(N, I) :- student(G, N, [M, C, O, P]),
   format("| ~a~t~5+ | ~a~t~5+ | ~a~t~16+ | ~a, ~a, ~a, ~a~t~41+ |~n", [I, G, N, M, C, O, P]).

printTHead :-
   printLine(),
   format("| ~a~t~5+ | ~a~t~5+ | ~a~t~16+ | ~a~t~40+ |~n", ['#', group, name, grades]),
   printLine().

printLine :- format("|~`-t~70||~n").

printList([S|T], N) :- printStudent(S, N), printLine(), K is N + 1, printList(T, K).

printFaildStudents :- printTHead(), setof(N, hasBadGrades(N), L), printList(L, 1).

student(1050, adams, ['math/4', 'c++/3', 'oop/4', 'physics/3']).
student(2100, alexander, ['math/3', 'c++/2', 'oop/4', 'physics/3']).
student(3151, allen, ['math/3', 'c++/4', 'oop/3', 'physics/3']).
student(4200, anderson, ['math/3', 'c++/2', 'oop/2', 'physics/3']).
student(5250, bailey, ['math/4', 'c++/3', 'oop/3', 'physics/4']).
student(1050, baker, ['math/3', 'c++/3', 'oop/4', 'physics/2']).
student(2100, barnes, ['math/4', 'c++/3', 'oop/3', 'physics/2']).
student(3151, bell, ['math/4', 'c++/3', 'oop/4', 'physics/4']).
student(4200, bennett, ['math/4', 'c++/3', 'oop/4', 'physics/3']).
student(5250, brooks, ['math/4', 'c++/3', 'oop/3', 'physics/3']).
student(1050, brown, ['math/3', 'c++/4', 'oop/4', 'physics/2']).
student(1050, flores, ['math/4', 'c++/3', 'oop/4', 'physics/3']).
student(2100, foster, ['math/4', 'c++/4', 'oop/3', 'physics/4']).
student(3151, garcia, ['math/3', 'c++/3', 'oop/2', 'physics/3']).
student(4200, gonzales, ['math/3', 'c++/4', 'oop/4', 'physics/3']).
student(5250, gonzalez, ['math/4', 'c++/2', 'oop/3', 'physics/3']).
student(1050, gray, ['math/3', 'c++/4', 'oop/3', 'physics/4']).
student(2100, green, ['math/2', 'c++/4', 'oop/1', 'physics/4']).
% . . .

enter image description here

New contributor
Dmitry Gashko is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

Your Answer

Dmitry Gashko is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.