[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

Identifying members in lists of lists.

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 Prolog...it doesn't seem like it should be anywhere near as difficult as it has been for me.

Any thoughts, suggestions?
0
Slapkey
Asked:
Slapkey
  • 2
1 Solution
 
fridomCommented:
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:
nth0(0,[1,2,3],Element).

Element = 1

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

nth1(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.

Regards
Friedrich
0
 
fridomCommented:
Small addition you might like to check one oneline tutorial for Prolog like:
http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html


Regards
Friedrich
0
 
SlapkeyAuthor Commented:
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!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now