RosettaCodeData/Task/Identity-matrix/Prolog/identity-matrix.pro

30 lines
744 B
Prolog
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
%rotates one list clockwise by one integer
rotate(Int,List,Rotated) :-
2026-02-01 16:33:20 -08:00
integer(Int),
length(Suff,Int),
append(Pre,Suff,List),
append(Suff,Pre,Rotated).
2023-07-01 11:58:00 -04:00
%rotates a list of lists by a list of integers
rotate(LoInts,LoLists,Rotated) :-
2026-02-01 16:33:20 -08:00
is_list(LoInts),
maplist(rotate,LoInts,LoLists,Rotated).
2023-07-01 11:58:00 -04:00
%helper function
append_(Suff,Pre,List) :-
2026-02-01 16:33:20 -08:00
append([Pre],Suff,List).
2023-07-01 11:58:00 -04:00
idmatrix(N,IdMatrix):-
2026-02-01 16:33:20 -08:00
%make an N length list of 1s and append with N-1 0s
length(Ones,N),
maplist(=(1),Ones),
succ(N0,N),
length(Zeros,N0),
maplist(=(0),Zeros),
maplist(append_(Zeros),Ones,M),
%create the offsets at rotate each row
numlist(0,N0,Offsets),
rotate(Offsets,M,IdMatrix).
2023-07-01 11:58:00 -04:00
main :-
2026-02-01 16:33:20 -08:00
idmatrix(5,I),
maplist(writeln,I).