1:- module(vector, [new_vector/1, new_vector/2, v_ref/3, v_size/2, v_max/2,
2 v_member/2, new_index/3, new_index/4]
3 ). 4
6
7new_vector($$(Size, 0, Array), Size) :-
8 functor(Array, ##, Size).
9
10new_vector(Vector) :- new_vector(Vector, 8).
11
13v_ref(I, $$(_, _, Array), R):- arg(I, Array, R).
14
15v_size($$(_, I, _), I).
16
17v_max($$(M, _, _), M).
18
20new_index(I, $$(Max, J, Array), $$(Max1, I, Array1)):-
21 succ(J, I),
22 ( J < Max -> Max1 = Max, Array1 = Array
23 ; plus(Max, Max, Max1),
24 functor(Array1, ##, Max1),
25 copy_args(Max, Array, Array1)).
27new_index(I, E, A, B):- new_index(I, A, B),
28 v_ref(I, B, E).
29
30v_member(X, $$(_, I, Array)):- between(1, I, J),
31 arg(J, Array, X).
32
34copy_args(0, _, _):-!.
35copy_args(I, X, Y):- arg(I, X, A), arg(I, Y, A),
36 J is I -1,
37 copy_args(J, X, Y).
38
40list_to_vector(L, $$(N, N, V)):- compound(L), !,
41 length(L, N),
42 V=..[(##)|L].
43list_to_vector(L, $$(_, J, V)):-
44 list_to_vector(1, J, L, [], V).
45
47list_to_vector(I, J, L, L, _):- I > J, !.
48list_to_vector(I, J, [A|L], M, V):-
49 arg(I, V, A),
50 succ(I, K),
51 list_to_vector(K, J, L, M, V)