Search code examples
traversaljessbag

Traversing the elements of a bag list


I wanted to know if it would be possible to traverse through the elements of a bag list and store them into a variable?

Thank you very much Ali

Edited by laune using the code given by OP in some comment

 (defglobal ?*Friends* = 0 ) 
 (bind ?*Friends* (bag create Bob))
 (bind ?*Friends* (bag create Nicolas))
 (bind ?*Friends* (bag create Nancy)) 
 (bind ?*Friends* (bag create John))
 (bind ?*Friends* (bag create George)))

now I want to go through this bag list and print any variable separately,


Solution

  • You cannot create a bag in this way, and I don't think you need a bag, which is a set of key-value pairs. What you are doing is to create a simple list, one that contains Bob, trying to do Nicolas, Nancy, John and George.

    A bag would associate a key with a value, e.g.

    (defglobal ?*Friends* = 0 )
    (bind ?*Friends* (bag create my-friends))
    (bag set ?*Friends* Bob "old and reliable")
    %%...
    (bag set ?*Friends* George "new and curious")
    

    To get all the names of the friends you'd write

    (bind ?names (bag props ?*Friends*))
    

    which returns a list (Bob ... George).

    But you could have created this list right away in the first place...