Dictionary/Hash problem

Notices and updates
Locked
hsmyers
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Dictionary/Hash problem

Post by hsmyers »

Given code that looks like this:

Code: Select all

(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:

Code: Select all

> 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 "ℑ♥λ"—Toto

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Post by hsmyers »

So it should be:

Code: Select all

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 "ℑ♥λ"—Toto

hsmyers
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Post by hsmyers »

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 "ℑ♥λ"—Toto

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Post by hsmyers »

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 "ℑ♥λ"—Toto

Locked