Hacker News new | past | comments | ask | show | jobs | submit login

Prolog relaxes the requirement to think linearly, bottom-up. If you write all the facts and predicates bottom-up it's easy in Python. Dictionaries, sets, and comprehensions take care of everything. As you say, it's trivial.

If you want to write top-down, defining what a mother is before defining who are parents and who are female, then you'll need to write functions and it gets trickier in Python. The sticking point is when to transition from functions to dict/set -- what's a predicate and what's a fact. We could get fancy with decorators to create some funky predicate class, but let's stay away from metaprogramming for the moment.

I agree that Python (without getting fancy) requires the programmer to think through a bit more of the inferential logic than Prolog would under some circumstances. How often do those circumstances arise in practical problems? I'm not sure.

However, I've got to accept the challenge: "What is a list with 30 unique integer elements that sum to 100?"

First, there is no list with 30 unique positive integers that sum to 100.

    >>> sum(range(30))
    435
Allowing negatives would create an infinity of possible solutions. So, I'm going to rephrase the problem: Generate a list of N unique positive integers that sum to M.

    >>> from itertools import combinations
    >>> m = 30
    >>> n = 5
    >>> combos = (c for c in combinations(range(m), n) if sum(c) == m)
    >>> next(combos)
    (0, 1, 2, 3, 24)



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: