combinations of variable length nested lists

Alex Martelli aleaxit at
Tue Aug 7 21:35:46 CEST 2001

"Mark Robinson" <m.1.robinson at> wrote in message
news:3B7023A4.6020408 at
> Joseph A. Knapka wrote:
> > PEP proposal: add a backtracking Horn-clause resolution
> > engine to Python!
> >
> >  :-)
> >
> > This problem is a two-liner in Prolog:
> >
> > one_of_each([],[]).
> > one_of_each([H|T],[H1|Ts]) :- member(H1,H), one_of_each(T,Ts).
> backtracking Horn-clause resolution engine, that can't be real
> terminology, I am sure you just plagerised Elmer Fudge :)

The terminology is right, but the Prolog example he gives looks
just like a transliteration of a ML or Haskell program and uses no
backtracking that I can see.  The imperative equivalent:

def one_of_each(items, containers):
    for item, container in zip(items, containers):
        if not item in container: return 0
    return 1

or the high-level-functional one:

def one_of_each(items, containers)
    import operator as op
    return reduce(op.and, map(op.contains, containers, items))

appear to be at least as good to me as the recursive one...


More information about the Python-list mailing list