Saturday, 15 September 2012

Adding atoms to lists prolog -


I'm trying to get my head about the concept I am fairly new to Prolog and I lists. One example I'm trying to:

  value (A, 1). Value (B, 2) Value (A, 3) Value (A, 4) Value (C, 3)   

I am trying to make a bill that is Searched (A, list) in the results: list = [1, 3, 4].

It depends only on all the facts and adds the matching people to the list.

I have tried to do something like that. But it does not seem right:

  find (x, list): - value (x, d), app ([d], [], [list | rest]), search (x , [Head | list]).   

Sorry about such a basic question. Any guidance is appreciated.

Note: I intend to expand the program by adding all values ​​to the list.

I believe whatever is trying to do is already present, it Try:

 
code>? - Search (X, Value (A, X), list).

I have not been installed on this machine, so I can not test it completely, but you should find what you are looking for.

EDIT: Sorry, managed to join swi and I realized that I had two arguments inverted, now see the updated code. Apart from this it was tested and it works: -)

Edit 2: According to the above comment, you can assemble those values ​​that match 'A' and the amount Which you can add to:

 ? - findall (x, value (a, x), list), summary (list, sum).   

- Ken

No comments:

Post a Comment