Dictionary/Hash problem

Started by hsmyers, February 20, 2008, 09:17:02 AM

Previous topic - Next topic

hsmyers

Given code that looks like this:


(context 'board)
(define (board:board key value)
  (if value
    (context 'board key value)
    (context 'board key)))

(define (init-board
  (board "a1" (list "r" "w" "w"))
  (board "b1" (list "n" "w" "b"))
  (board "c1" (list "b" "w" "w"))
  (board "d1" (list "q" "w" "b"))
  (board "e1" (list "k" "w" "w"))
  (board "f1" (list "b" "w" "b"))
  (board "g1" (list "n" "w" "w"))
  (board "h1" (list "r" "w" "b"))))

(context MAIN)

why do I get this:




> board
(lambda (key value)
 (if value
  (context 'board:board key value)
  (context 'board:board key)))
(lambda ((board:board "a1" (list "r" "w" "w")) (board:board "b1" (list "n" "w" "b"))
  (board:board "c1" (list "b" "w" "w"))
  (board:board "d1" (list "q" "w" "b"))
  (board:board "e1" (list "k" "w" "w"))
  (board:board "f1" (list "b" "w" "b"))
  (board:board "g1" (list "n" "w" "w"))
  (board:board "h1" (list "r" "w" "b"))))
MAIN
> (board "a1")
nil
>



--hsm
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

cormullion

#1
Hi there, hsm! I can see a few possible problem areas.



First, is there a missing parenthesis after (define (init-board ?



Then, when is (init-board) being called? I can see it defined, but not called.



Also, typing board rather than (board) at the prompt will give different results...

hsmyers

#2
So it should be:


define (init-board)
  (board "a1" (list "r" "w" "w"))
  (board "b1" (list "n" "w" "b"))
  (board "c1" (list "b" "w" "w"))
  (board "d1" (list "q" "w" "b"))
  (board "e1" (list "k" "w" "w"))
  (board "f1" (list "b" "w" "b"))
  (board "g1" (list "n" "w" "w"))
  (board "h1" (list "r" "w" "b")))


instead?



Didn't type board, that was from evaluating the file buffer...



--hsm
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

hsmyers

#3
The paren changes and the missing (board:init-board) were precisely the cure! Much thanks cormullion. I'd been so busy getting no where that I slipped up and left the call out!



--hsm
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

cormullion

#4
I've just realised that your code looks a bit like a chess setup. (I'm slow sometimes...) Are you writing some chess routines in newLISP? If so, it sounds like a cool project - tell us more!

hsmyers

#5
Yes this is chess code. I'm converting my CPAN modules to newLISP. I use this kind of code as a learning project for languages new to me and those that I haven't used in a long time. Closest thing to this was a similar effort in scheme.



In general this comes down to parsing chess notation (algebraic at first, English is a pain in the ass so it comes later) and  doing various things with the result. Given input in PGN, I can convert to LaTeX, html, etc. Among other things I can identify the opening in various styles: tradition name, ECO, etc. Further I can extract all of the positions in a game for database use. I've even written code that uses a chess-engine's evaluation to annotate the games.



It (unlike the game itself) is all good fun!



--hsm
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto