This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/library/dialect/bprolog/arrays.yap
2011-11-03 07:54:51 +09:00

36 lines
862 B
Prolog

:- module(bparrays, [new_array/2, a2_new/3, a3_new/4, is_array/1, '$aget'/3]).
:- use_module(library(lists), [flatten/2]).
new_array(X, Dim.Dims) :-
functor(X, '[]', Dim),
recurse_new_array(0, Dim, Dims, X).
recurse_new_array(_, _, [], _X) :- !.
recurse_new_array(Dim, Dim, _Dims, _X) :- !.
recurse_new_array(I0, Dim, Dims, X) :-
I is I0+1,
arg(I, X, A),
new_array(A, Dims),
recurse_new_array(0, Dim, Dims, X).
a2_new(X, Dim1, Dim2) :-
functor(X, '[]', Dim1),
recurse_new_array(0, Dim1, [Dim2], X).
a2_new(X, Dim1, Dim2, Dim3) :-
functor(X, '.', Dim1),
recurse_new_array(0, Dim1, [Dim2,Dim3], X).
is_array(X) :-
functor(X, '[]', _Dim).
'$aget'(A,[],A).
'$aget'(A,I.Is,A) :-
arg(I, A, X),
'$aget'(X,Is,A).
array_to_list(A, List) :-
flatten(A, List).