value expected in function??

Started by hsmyers, February 21, 2008, 04:09:34 PM

Previous topic - Next topic

hsmyers

I've written myself into a problem. The result after (board:find-king "w") is:


...
candidate is: f8
candidate is: f7
candidate is: f6
candidate is: f5
candidate is: f4
candidate is: f3
candidate is: f2
candidate is: f1
candidate is: e8

value expected in function = : square
called from user defined function board:white-piece?
called from user defined function board:k?
called from user defined function board:find-king


Relevant code looks like:


(define (side square)
((board square) 1))

(define (board:board key value)
  (if value
    (begin
(push key keylist)
(context 'board key value))
    (context 'board key)))

(define (white-piece? square)
(= "w" (side square)))

(define (find-king side)
(if (= side "w")
(setq k? white-king?)
(setq k? black-king?))
(catch
(dolist (candidate keylist)
(println "candidate is: " candidate)
(if (k? candidate)
(throw candidate)))))


All appears to work fine until we hit "e8" in the keylist. At that

point we get the above error message. I'm not entirely sure what it

is complaining about; that square has no value or that a function called

square was expecting a value?



Note that if I rewrite white-piece? to look like:


(define (white-piece? square)
(= "w" ((board square) 1)))


...all works fine! Efforts to trap this are problematic because it appears

to die somewhere after the call but before the body of the function!



Clues and or clue-sticks would be nice!



--hsm

p.s. I believe my addition to the default function for board solves the 'key' problem...
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

cormullion

#1
Hi again. Looks like you're making rapid progress! Already I can't understand what you're up to... :)



I can't run the code extract - too many missing defs. But I'll hazard a guess that the error message means that the "=" operator was expecting a value but didn't get one...



Are you going to write chess-playing code too? That would be an interesting project...

hsmyers

#2
Found the problem! Can't use a parameter with the same name as a function (duh!); note the use of 'side' as both. If I change either the parameter's name or the function's name then the code works as intended. Had it been a snake I'd have been dead!



Corrected looks like:
(define (find-king which-side)
(if (= which-side "w")
(setq k? white-king?)
(setq k? black-king?))
(catch
(dolist (candidate keylist)
(if (k? candidate)
(throw candidate)))))

Although I've no idea why use of a symbol as parameter should 'step' on the use of a symbol as function?



I'm into parsing chess games rather than 'playing', although the code could be the basis of such a thing. Perhaps later...
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto