Hacker News new | ask | show | jobs
by S4M 3772 days ago
I can somewhat make sense of that code, but the code of function ch-hof is a bit hard to read - and the function name is not helping. I guess he's trying to see if the king is under check, and if so by which piece(s).

Also I notice code like:

    (- (cdr kpos) 1))
How is that possible? If kpos is a list of two elements to represent the king's position, so (cdr kpos) should be a list, not compatible with subtraction.

For example:

    #;4> (- (cdr '(1 2)) 1)

    Error: (-) bad argument type: (2)
2 comments

The lists are made of cons, but you can make arbitrary trees with cons.

After looking carefully, the kpos is the result of the function find. In this case it's a cons with the row and the column of the king. So the code that runs is more like

  (define kpos (cons 1 2))
  (- (cdr kpos) 1)
In this case, for clarity I prefer to use a struct instead of a cons to save the position

  (struct cell (row col))
  (define kpos (cell 1 2))
  (- (cell-col kpos) 1)
kpos is a pair, not a list (or if you prefer, is an improper list). It's (cons 1 2), while '(1 2) is (cons 1 (cons 2 empty)).