Arc Forumnew | comments | leaders | submitlogin
problem combining each with a list of tables
3 points by bOR_ 6104 days ago | 4 comments
using each on a list of tables, I'm trying to access a field from each table, as it passes through 'each'. Somehow it seems that accessing tables throuch each isn't the same as accessing them directly.

  $ arc
  (= airports (table))
  #hash()
  (= (airports "Boston") 'bos)
  bos

  arc> (each c '(airports) (prn c))
  airports
  nil
  arc> (prn airports)
  #hash((Boston . bos))
  #hash(("Boston" . bos))
  arc> (each c '(airports) (prn (c "Boston")))
  Error: "Function call on inappropriate object airports (\"Boston\")"
As you see, (prn mytable) gives a different result than (each c lst (prn c)), and I can't access the tables anymore. Am I on using the wrong syntax?


5 points by cchooper 6104 days ago | link

You're passing '(airports) to each, which is a list containing the symbol airports, not the value of that symbol.

  arc> (car '(airports)) 
  airports
What you need to do is this:

  (each c (list airports) (prn c))

-----

4 points by bOR_ 6104 days ago | link

Thanks! This now works, and I'm learning :)

  ; makes a world matrix
  (def makeworld (x)
    (= world (n-of x (n-of x nil))) ; world of nil
    (= merged (n-of x (n-of x nil))) ; layer of movers
    nil)


  ; print out the world, imposing a list of critters
  ; on top of it. The list itself assumes that its entries
  ; are tables which have a symbol and a position field
  (def show (lst)
    ; copy world into merged
    (for y 0 (- (len world) 1)
      (for x 0 (- (len world) 1)
        (= ((merged y) x) ((world y) x))))
    ; superimpose critters
    (each c lst
      (= ((merged (car (c "pos"))) (cadr (c "pos"))) (c "symbol")))
    ; plot world
    (each row merged
      (each pos row (if (is pos nil) (pr #\. #\space) (pr pos #\space)))
      (prn)))

  (= creature (table))
  (= creature2 (table))
  (= creature3 (table))
  (= (creature "pos") '(1 3))
  (= (creature2 "pos") '(10 3))
  (= (creature3 "pos") '(8 8))
  (= (creature "symbol") #\$)
  (= (creature2 "symbol") #\@)
  (= (creature3 "symbol") #\#)
  (makeworld 15)

  arc> (show (list creature creature2 creature3))
  . . . . . . . . . . . . . . . 
  . . . $ . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . . . . . . # . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . @ . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  . . . . . . . . . . . . . . . 
  nil

-----

2 points by cchooper 6103 days ago | link

Nice! Is that going to be a game or something?

-----

1 point by bOR_ 6072 days ago | link

http://wwww.bagofsouls.com

It's the start of a new project for me (and hopefully later on a postdoc position on the topic). The idea is to build some kind of smart animals in an artificial world, without having to design them, or run through countless of iterations with genetic algorithms to get one.

With a good basic structure, they should be able to get smart on their own ;).

-----