From 83a3c52a7513f6cca322707f35a5abe3db9d5018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Tue, 15 Dec 2015 09:05:24 +0000 Subject: [PATCH] move length to lists --- pl/lists.yap | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ pl/sort.yap | 41 ----------------------------------------- 2 files changed, 52 insertions(+), 41 deletions(-) diff --git a/pl/lists.yap b/pl/lists.yap index a6fce74ab..ea0e69657 100644 --- a/pl/lists.yap +++ b/pl/lists.yap @@ -1,3 +1,13 @@ +/** + * @file pl/lists.yap + * @author VITOR SANTOS COSTA + * @date Thu Nov 19 09:54:00 2015 + * + * @brief core lisy operations + * + * @ingroup lists + * @{ +*/ :- system_module( '$_lists', [], []). @@ -88,4 +98,46 @@ lists:delete([Head|List], Elem, [Head|Residue]) :- :- set_prolog_flag(source, false). % disable source. + + +% length of a list. + +/** @pred length(? _L_,? _S_) + + +Unify the well-defined list _L_ with its length. The procedure can +be used to find the length of a pre-defined list, or to build a list +of length _S_. + +*/ + +prolog:length(L, M) :- + '$skip_list'(L, M, M0, R), + ( var(R) -> '$$_length'(R, M, M0) ; + R == [] + ). + % +% in case A1 is unbound or a difference list, things get tricky +% +'$$_length'(R, M, M0) :- + ( var(M) -> '$$_length1'(R,M,M0) + ; M >= M0 -> '$$_length2'(R,M,M0) ). + +% +% Size is unbound, generate lists +% +'$$_length1'([], M, M). +'$$_length1'([_|L], O, N) :- + M is N + 1, + '$$_length1'(L, O, M). + +% +% Size is bound, generate single list +% +'$$_length2'(NL, O, N) :- + ( N =:= O -> NL = []; + M is N + 1, NL = [_|L], '$$_length2'(L, O, M) ). + +%% @} + diff --git a/pl/sort.yap b/pl/sort.yap index 908c21009..8fcce79b5 100644 --- a/pl/sort.yap +++ b/pl/sort.yap @@ -37,47 +37,6 @@ */ -% length of a list. - -/** @pred length(? _L_,? _S_) - - -Unify the well-defined list _L_ with its length. The procedure can -be used to find the length of a pre-defined list, or to build a list -of length _S_. - - - - - */ -length(L, M) :- - '$skip_list'(L, M, M0, R), - ( var(R) -> '$$_length'(R, M, M0) ; - R == [] - ). - -% -% in case A1 is unbound or a difference list, things get tricky -% -'$$_length'(R, M, M0) :- - ( var(M) -> '$$_length1'(R,M,M0) - ; M >= M0 -> '$$_length2'(R,M,M0) ). - -% -% Size is unbound, generate lists -% -'$$_length1'([], M, M). -'$$_length1'([_|L], O, N) :- - M is N + 1, - '$$_length1'(L, O, M). - -% -% Size is bound, generate single list -% -'$$_length2'(NL, O, N) :- - ( N =:= O -> NL = []; - M is N + 1, NL = [_|L], '$$_length2'(L, O, M) ). - /** @pred sort(+ _L_,- _S_) is iso