Notes on Jason

In this post, I will list some ideas I got about Jason from the Simulation module in the course at the university of Bologna. Specifically, I will focus on these ways of writing agentspeak code that is correct but looks not natural to those who are knowedgable (for me, it’s good ‘ole Fran who first helped me to the exoteric aspects of this way of thinking.

So, without further ado, let’s list some classic “mistakes.” We start from code written from two excellent students. We have a list of [value, key], for example, [[1,a][2,b][3,c]] and a list of names, [ a,c, d]. We want to keep items in the first list only if they have the name in second. The result must be sorted.

Solution 1:

!updateList([[1,a][2,b][3,c]], [a,c,d], []).

+!updateList(Couples, Names, Res) <-
	.length(Couples, CouplesLength);
	//CouplesLength = CouplesLength; // ??
	if (CouplesLength > 0) {
		.nth(0, Couples, CurrentCouple)
		.delete(0, Couples, TailCouples);
		.nth(1, CurrentCouple, CurrentName); 
		if (.member(CurrentName, Names)) {
			.concat([CurrentCouple], Res, NewRes);
			!updateList(TailCouples, Names, NewRes);
		} else {
			// do nothing, thus losing the current couple.
			!updateList(TailCouples, Names, Res);
		}
	}
	else {
		// ultima esecuzione del piano per il ciclo in corso
		.sort(Res, SortedRes);
		.print(SortedRes);
	}
	.

This solution works – but * it doesn’t use the context, instead it uses nested if as if we were in java; * it doesn’t process the list in the logical [H|T] way; * accesses elements inside a list with .nth instead of just unifying them.

A solution that does the same but only using logical style programming is the following:

!updateList([[1,a][2,b][3,c]], [a,c,d], []).

// update the ones I know.
+!updateList([[Value, Name]| T], Names, Res): .member(Name, Names) <-
	.concat([[Value, Name]], Res, NewRes);
	!updateList(T, Names, NewRes).

// drop the ones I don't know.
+!updateWages([[Value, Name]| T], Names, Res) <-
	!updateList(T, Old, Res).

// wrap it up	
+!updateWages([], Names, Res) <-
		.sort(Res, SortedRes);
		.print(SortedRes).

Which one is more readable? Which one is easiest to maintain?

← Older Newer →