0

I want to write all of a list in prolog. But the problem is I don't want to use recursion. So I want to to this iteratively.

lurker
  • 56,987
  • 9
  • 69
  • 103

2 Answers2

2

Strictly speaking, there is no way how you can avoid recursion when you want to express some relation about all (or sufficiently many) elements in a list. However, you might delegate the recursive part to some library predicate.

Take the maplist-family as an example. Say maplist(=(_), Xs) which describes all lists whose elements are the same. To you, there is no longer any recursion. But behind, there is a recursive definition:

maplist(_C_1, []).
maplist(C_1, [E|Es]) :-
   call(C_1, E),
   maplist(C_1, Es).

There are also other ways to realize shortcuts for recursive predicates, like do-loops and B-Prolog's loops. But ultimately, they all translate to some recursive definition (or call recursive predicates).

There is really no reason to worry about using recursion in Prolog. After all, systems are quite optimized to handle recursion efficiently. Often, recursive predicates "run like" simple loops in imperative programming languages. Also, memory allocation is very well targeted to clean up intermediary/volatile data.

Community
  • 1
  • 1
false
  • 10,264
  • 13
  • 101
  • 209
-1

With Swi-Prolog, I have been experimenting with the fact that findall/3 unifies it's last list-argument, aka. it can "run on reverse" or input/output swapped, something like this findall(,,[a,b,c])

Then I came up with this:

Li=[a,b,c,d,e,f,g,h],findall(A, (append(A,B,Li),B=[C|_],writeln(C)), _).

A and B gets instantiated to sublists

Even this works!!

Li=[a,b,c,d,e,f,g,h],findall(_, (append(_,B,Li),B=[C|_],writeln(C)), _).

http://swish.swi-prolog.org/p/oMEAdQWk.pl

Well I don't know how efficient the code is, propably is not efficient. And you should first learn the recursive rules of Prolog :)

re Paul
  • 122
  • 1
  • 8