add flatten.

This commit is contained in:
Mark Thom
2018-03-26 18:49:43 -06:00
parent a937eb4d8c
commit 2691382771
2 changed files with 11 additions and 2 deletions

View File

@@ -136,6 +136,7 @@ The following predicates are built-in to rusty-wam.
* `display/1`
* `duplicate_term/2`
* `false/0`
* `flatten/2`
* `float/1`
* `functor/3`
* `ground/1`

View File

@@ -1,5 +1,5 @@
:- module(lists, [member/2, select/3, append/3, is_list/1, memberchk/2, reverse/2, maplist/2,
maplist/3, maplist/4, maplist/5, maplist/6, maplist/7, maplist/8,
flatten/2, maplist/3, maplist/4, maplist/5, maplist/6, maplist/7, maplist/8,
maplist/9]).
member(X, [X|_]).
@@ -11,7 +11,7 @@ select(X, [Y|Xs], [Y|Ys]) :- select(X, Xs, Ys).
append([], R, R).
append([X|L], R, [X|S]) :- append(L, R, S).
is_list(X) :- var(X), !, false.
is_list(X) :- var(X), !, false.
is_list([]).
is_list([_|T]) :- is_list(T).
@@ -24,6 +24,14 @@ reverse([], Ys, Ys).
reverse([H|T], Ps, Rs) :-
reverse(T, [H|Ps], Rs).
flatten([], List, List) :- !.
flatten([Head|Tail], List0, List) :- !,
flatten(Head, List0, List1),
flatten(Tail, List1, List).
flatten(Other, [Other|List], List).
flatten(Tree, List) :- flatten(Tree, List, []).
maplist(_, []).
maplist(Cont1, [E1|E1s]) :-
call(Cont1, E1),