Identifying members in lists of lists.

Posted on 2007-10-13
Last Modified: 2008-01-09
Hi.  I am very new to Prolog, but I'm trying to learn it.
I am more famiilar with C, and wrote a program whose output gave me a list of connections between nodes in the form of a matrix, which in its simplest case looks like this:

[ -1,  1]
[  1, -1]

In the example above, Row 1 is Node 1's connections, and -1 means NO connection exists.  Any non-"-1" value means a connection exists.  So in the example above, Node 1 is connected to Node 2 (and Node 2, or Row 2, is subsequently connected to Node 1).

What I'm trying to do in prolog is to be able to supply this list of lists as a variable, say "Data," and have Prolog give me back each node's connections.  
If I give it [-1, 1, 1, 1, -1, -1, 1], I want it to give me the list [Node#, 2, 3, 4, 7], where node# increments as it goes through the lists of Data.

Does that make sense?

I have been against a brick wall for about 3 hours now, trying different things out, but I can never get my rules and predicates to give me what I want.  

I can get the list of lists to get bound correctly, but I cannot come up with something that can get a new list of lists created with the data I want...

Like I said I am very much a beginner at doesn't seem like it should be anywhere near as difficult as it has been for me.

Any thoughts, suggestions?
Question by:Slapkey
    LVL 24

    Accepted Solution

    Prolog is really unique, and one is not used to it. But here we go:
    connected(List, Counter, Accum, Result) :-
        List = [],
        reverse(Accum, Result).
    connected(List, Counter, Accum, Result) :-
        List = [Head|Tail],
        NCounter is Counter + 1,
        (Head = 1 ->
            connected(Tail, NCounter, [Counter|Accum],Result);
            connected(Tail, NCounter, Accum,Result)

    Output with this call:
     connected([-1, 1, 1, 1, -1, -1, 1], 1, [], Result).

    Result = [2, 3, 4, 7]

    You can not generally say where the index starts. See e.g:

    Element = 1

    but also
    nth(1, [1,2,3], Element).


    Element = 1

    The main difference is not that you have to declare how you like to get things done, you give it some goals to achive. The goal in your part in you example is a list which collects the indexed of another list you pass....

    You should not try to program Prolog as you do an imperative language.

    LVL 24

    Expert Comment

    Small addition you might like to check one oneline tutorial for Prolog like:

    LVL 2

    Author Comment

    Thank you very much!  I have been able to take what you gave me and to continue building on it to try and achieve my end-goal with this data.  Hopefully I will be able to complete it now!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now