Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

SQL has no recursion. I would argue that's a pretty fundamental aspect of Prolog, so I don't think SQL is a representative replacement.


Yes it does. It’s a fun problem to make a sudoku solver

https://technology.amis.nl/it/solving-a-sudoku-with-one-sql-...


Ok fair enough. I suspect that's a pretty non-standard/rarely-used feature though. If you learn SQL you likely won't encounter this and I still contest the idea that SQL is a good entry into the logic paradigm.

SQL and Prolog are both relational. That's very unique to both. But SQL is all about querying databases. Prolog can also be used and understood as a database querying language but it's also very strong for

- parsers

- interpreters

- expert systems

- solving combinatorial problems

If you really want to you can probably use SQL for that too. Or any language for that matter. But going out and learning SQL won't naturally expose you to these applications and how well the logic paradigm lends itself to them.


> I suspect that's a pretty non-standard/rarely-used feature though. If you learn SQL you likely won't encounter this

Recursive common table expressions are part of the SQL standard (since 1999) and are quite frequently used to traverse hierarchical data (aka "adjacency list").

It is part of basically all (good) SQL tutorials - at least in the "advanced" part.


I don't remember using recursion in a real project, but I built a HN clone on top of Postgres, with the following query:

    WITH RECURSIVE thread(id, parent_id, user_id, post_id, timestamp, text, depth) AS (
      SELECT id, parent_id, user_id, post_id, timestamp, text, 0
      FROM comments
      WHERE user_id = 1
        AND parent_id IS NULL
      UNION ALL
      SELECT c.id, c.parent_id, c.user_id, c.post_id, c.timestamp, c.text, t.depth + 1
      FROM comments c
      JOIN thread t ON c.parent_id = t.id
      WHERE c.user_id != t.user_id
    )
    SELECT * FROM thread ORDER BY timestamp ASC;


I wrote a recursive CTE to do a tree traversal on a parent-child “relationship” table a few weeks ago at work.

They do come up!


Oh, it's certainly not a "type specimen". It does actually have recursion and matching-like behavior, but those are convoluted and not really used, so it is not very suitable for general programming.

But lacking those general features makes it an even purer "learning example" of the group.




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

Search: