newLISP Fan Club

Forum => newLISP newS => Topic started by: hsmyers on February 20, 2008, 09:17:02 AM

Title: Dictionary/Hash problem
Post by: hsmyers on February 20, 2008, 09:17:02 AM
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
Title:
Post by: cormullion on February 20, 2008, 09:52:35 AM
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...
Title:
Post by: hsmyers on February 20, 2008, 11:00:04 AM
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
Title:
Post by: hsmyers on February 20, 2008, 12:21:30 PM
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
Title:
Post by: cormullion on February 21, 2008, 01:29:33 AM
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!
Title:
Post by: hsmyers on February 21, 2008, 02:37:12 PM
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