ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

I just registered into this forum, been using linux (ubuntu) for over a year now - am happy with it too! -, and I'm trying to spend this summer to learn more* on programming right before I go to uni, where they would teach it anyways ...

*more than the basics of Pascal, which I was taught in secondary

So, my problem is that I've been reading a prolog introductory book, and in it was an exercise which I have failed to solve thus far - which continued for days, now.

Here's the riddle:

Quote:

Binary trees are trees where all internal nodes have exactly two childres. The smalles binary trees consist of only one leaf node. We will represent leaf nodes as leaf(Label). For instance, leaf(3) and leaf(7) are leaf nodes, and therefore small binary trees. Given two binary trees B1 and B2 we can combine them into one binary tree using the predicate tree: tree(B1,B2). So, from the leaves leaf(1) and leaf(2) we can build the binary tree tree(leaf(1), leaf(2)). And from the binary trees tree(leaf(1), leaf(2)) and leaf(4) we can build the binary tree tree(tree(leaf(1), leaf(2)), leaf(4)).

Now, define a predicate swap/2, which produces a mirror image of the binary tree that is its first argument. For example:

EDIT: my problem, more specifically said, is that I cannot make the predicate for binary trees with 2 nodes and the predicate for the single leaf node, which still counts as a tree, count as equal. Namely, tree/2 and tree/1 are two different things for prolog. Which means that I can only represent complex trees using these specific rules:

<edit> Please see following post </edit>
I have not used Prolog professionally. I had a brief encounter with it in school. That was a long time ago.
I have not tested this, but I think you could implement swap something like this:

Ummm... the thing I posted here isn't something specific, like classes, polimorphism and other stuff I read when I skimmed through a C++ book; they are just predicates. Rules, facts, clauses. They're as fundamental as variables in imperative languages. What I'm trying to say, is that by searching for something like 'data structures', or 'trees', won't help me much, because these are just labels. One could name their predicates and their arguments anything, like cat(tom, bob), or tree(leaf(1), leaf(2)); they don't mean anything, even the logical link is only in your head (in the sense that mother(caroline, ryan) and mother(ryan, caroline) may mean the same in different programs, if used consistently). But thanks anyway .

Sorry for going overboard about prolog, oh, and if a more adept initiated is reading this, feel free to smack me in the head for being so sloppy .

I'll be thinking about this more later, and post the solution for those who may be reading the same (free&online) book and stumble onto the same problem.

stuff I read when I skimmed through a C++ book ... like classes ... variables ...

You're absolutely correct:

* "Imperative, object-oriented languages" like C++, Java and C# have classes, variables, operations, control structures and other fundamental "building blocks" from which you can build higher-level constructs like trees, queues and, ultimately, applications and systems.

* "Declarative languages" like Prolog, on the other hand, have predicates, variables, lists, etc as their "fundamental building blocks". From which you can ALSO build higher-level constructs like trees, queues and, ultimately, applications and - who knows - maybe even full-blown systems.

The point is that Googling for something like "prolog data structures trees queues" CAN give you concrete examples for how to use Prolog syntax to build a logical "tree" data structure.

Just like the examples I cited, or the example ArthurSittler gave you.

Btw, I'm trying to advance your idea of recursion (that's the whole purpose of this exercise in the book - I just couldn't find a way to make it recursive). I'll post my results shortly.

[1-2 hrs after I forgot to send this post:]

I found it, finally. It's not pretty, because it covers all possibilities of how could a tree look like (e.g. tree(leaf(1)) or tree(leaf(1), leaf(2))). Anyways, the source code is in the attachment. What the swap part does is that it first checks whether the tree() has any nodes which have even more nodes (first predicate becomes true when it's a simple tree, with no internal nodes; second and third predicate becomes true when either the first or the second node of the main tree has nodes; fourth when both nodes of main tree() have nodes of their own). Then it tries t swap the internal nodes (if any), and finally swaps the two nodes of the main tree(). And this is recursive.

Thanks for both of your help, I was taught a small lesson in both programming and ethics

EDIT: I made a slight change to the file, found a small error in it (size is 1.2KB now).

Thank you, steve296.
After I installed Prolog, I was unable to get my proposed solution to work. In my defense, my brush with Prolog was very brief, and it was a long time ago. You have inspired me to refresh my Prolog skills. Do not be surprised if you get a question or two from me about Prolog in the near future.

Thank you, steve296.
After I installed Prolog, I was unable to get my proposed solution to work. In my defense, my brush with Prolog was very brief, and it was a long time ago. You have inspired me to refresh my Prolog skills. Do not be surprised if you get a question or two from me about Prolog in the near future.

Cool . I'm glad you found something to rediscover and cherish. Just don't let the notion vanish, though . And I'll be welcoming any question or thought about prolog, or anything for that matter.

Quote:

Originally Posted by paulsm4

Steve296 -

As ArthurSittler already mentioned, "Programming in Prolog", by Clocksin and Mellish is *the* resource for learning Prolog: