Determinate (Prolog)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Prolog | Python
<<determinate.pl>>=
vget([X|_], 0, X).
vget([_|T], I, X) :-
	I > 0,
	I1 is I-1,
	vget(T, I1, X).

mget([H|_], 0, J, X) :-
	vget(H, J, X).
mget([_|T], I, J, X) :-
	I > 0,
	I1 is I - 1,
	mget(T, I1, J, X).

numberOfRows(Matrix, Rows) :-
	length(Matrix, Rows).

numberOfColumns([Vector|_], Columns) :-
	length(Vector, Columns).

determinate(Matrix, Determinate) :-
	numberOfRows(Matrix, Rows),
	numberOfColumns(Matrix, Columns),
	Rows = 2,
	Columns = 2,
	mget(Matrix, 0, 0, A),
	mget(Matrix, 0, 1, B),
	mget(Matrix, 1, 0, C),
	mget(Matrix, 1, 1, D),
	Determinate is A * D - B * C.

Download code
hijacker
hijacker
hijacker
hijacker