1
I’m a beginner in Prolog and I have doubts about the list manipulation and the sum of its items.
I have a predicate historico(RA,[i1,i2,i3,...,in])
where ra
is the Academic Record of a student and each i
is an item, with the shape item(CM,SM,AN,NT,FQ)
, whereas CM
is the code of the course, SM
is the semester, AN
is the year, NT
the note, and FQ
the frequency.
In addition, the predicate curriculo(Codigocurso,[i1,i2,...,in])
where each i
is the code of a subject, shows us the subjects of each course.
The predicate materia(Codigomateria,Nomemateria,Creditosmateria)
shows how many credits each subject has in its third parameter.
Suppose I have the following facts:
historico(08080808,[item(1,1,2008,3.0,0.77),item(1,2,2008,6.5,0.90),item(5,1,2009,8.0,0.80)]).
materia(1,algoritmos_e_programacao_para_computadores_1,4).
materia(2,paradigmas_de_programacao,4).
materia(3,programacao_orientada_a_objetos,4).
curriculo(1,[1,2,3]).
I want to do a function that checks the percentage of credits already done by a certain student.
For that, I will have the function porcentagemcreditos(RA,Codigocurso,Porcentagemjacumprida)
.
I first need to add the credits of all the courses of the student (considering the subjects present in the curriculum), but I do not know how to add the items of a list so that I have the total credits of a course.
In addition, the function porcentagemcreditos
should disregard subjects that are extracurricular, that is, if he has taken any course that is not on his course curriculum.
To put it in context, I have the following rules that I’ve already created, but I don’t think they’ll be necessary, just the rules that I’ve already mentioned:
curso(CODIGOCURSO,NOMECURSO).
materia(CODIGOMATERIA,NOMEMATERIA,CREDITOSMATERIA).
curriculo(CODIGOCURSO,[CODIGOMATERIA1,CODIGOMATERIA2,...,CODIGOMATERIAn).
aluno(RA,NOME).
cursa(RA,CODIGOCURSO).
historico(RA,[ITEM1,ITEM2,...,ITEMn). (como mostrei anteriormente).
pertence_curso(M,C):-curriculo(C,Lista),member(M,Lista).
In the first function of summing, for example, I did not understand what "|R" means in "[element(,,N,,)|R]". For example, how I would test in Swi-Prolog the following function: > add([element(,,N,,)|R], Total). I didn’t understand how to pass to the function the list of elements.
– Gabriel Polo
A Prolog list is a chained list, with a head (the 1° element) and a tail (the remaining sublist). Example:
[1,2,3]
is the same as[1|[2|[3|[]]]]
. Therefore, a recursive call in a list handles the first element and then the "rest of the list", until it reaches the empty list[]
.[Primeiro|Resto]
. As for the example, try first to make a function that sums only numbers, then modify it to accept compound terms (for example, the history items). Ex.:?- somar ([item(1,2), item(2,3), item(1,5)], Total).
Choose which index you want to add.– mgibsonbr