help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Program problem


From: Lewis Creary
Subject: Program problem
Date: Wed, 29 Nov 2023 00:16:46 +0000 (UTC)

(It has been suggested to me by someone on the above mailing list thatthe 
following content would be appropriate for this list.)
My purpose here is to discuss a bug I've discovered in an emacs lispfunction, 
fill-rows, that I've written (shown below).  The main ideaof this function is 
to help solvers of Sudoku puzzles who want to keeptrack of the numbers they've 
already entered into a puzzle.  This willhelp them to backtrack when they 
change some of those numbers.  It'simportant to understand the documentation 
string of this function(included in the function definition) before reading the 
code.
The use case of interest involves this function call:   (fill-rows '(1 3 5)     
             '("123456789" "987654321" "123498765")                   '((0 0 0 
0 0 0 0 0 0)                    (0 0 0 0 0 0 0 0 0)                    (0 0 0 0 
0 0 0 0 0)                    (0 0 0 0 0 0 0 0 0)                    (0 0 0 0 0 
0 0 0 0)                    (0 0 0 0 0 0 0 0 0)                    (0 0 0 0 0 0 
0 0 0)                    (0 0 0 0 0 0 0 0 0)                    (0 0 0 0 0 0 0 
0 0)) )          
The desired value to be computed by this function call is:              '((1 2 
3 4 5 6 7 8 9)                 (0 0 0 0 0 0 0 0 0)                (9 8 7 6 5 4 
3 2 1)                (0 0 0 0 0 0 0 0 0)                (1 2 3 4 9 8 7 6 5)    
            (0 0 0 0 0 0 0 0 0)                (0 0 0 0 0 0 0 0 0)              
  (0 0 0 0 0 0 0 0 0)                (0 0 0 0 0 0 0 0 0))
The value (incorrectly) computed (this is the bug mentioned above) is:          
    '((0 0 0 0 0 0 0 0 0)                (9 0 0 0 0 0 0 0 0)                (0 
0 0 0 0 0 0 0 0)                (0 0 0 0 0 0 0 0 0)                (0 0 0 0 0 0 
0 0 0)                (0 0 0 0 0 0 0 0 0)                (0 0 0 0 0 0 0 0 0)    
            (0 0 0 0 0 0 0 0 0)                (0 0 0 0 0 0 0 0 0))
Here, then, is the function definition:
(defun fill-rows (row-nums change-strings pzl-form)  "This fn takes as 
arguments a list of row indices, a list of an equal   number, s, of 9-digit 
strings, and a puzzle form (i.e, an sexp of the form:                  ((n n n 
n n n n n n)                   (n n n n n n n n n)                   (n n n n n 
n n n n)                   (n n n n n n n n n)                   (n n n n n n n 
n n)                   (n n n n n n n n n)                   (n n n n n n n n 
n)                   (n n n n n n n n n)                   (n n n n n n n n 
n)), where n is a number between 0 and 9).   The fn returns as value t he 
result of filling in row m of the argument   puzzle form with numbers specified 
by the mth 9-digit string."  (let* ((row-index 0)           (row-num-index 0)   
        (row-str (nth row-index change-strings))           (row-num (nth 
row-num-index row-nums))           (change-nums nil)           (str-pos 0) )    
  (while (and (< row-index (length row-nums)) (< str-pos 9))             ;; 
(comment) cycle through the change-strings          (while (< str-pos 9)        
         ;; cycle through row-str, contributing to change-list                  
 (setq change-nums (append change-nums                                          
         (list (string-to-number                                                
      (substring row-str str-pos (1+ str-pos)) )) )                        
row-str (nth row-index change-strings) )              (setcar (car (nthcdr 
row-num pzl-form))                          (nth str-pos change-nums) )         
 (cl-incf str-pos) )       (cl-incf row-index)       (cl-incf row-num-index) )  
 pzl-form ) )
----------------------------------------------------------------------------------------------------
I'll very much appreciate receiving any proposed bugfixes or other  suggestions 
you may have.
Thanks.
Lewis Creary




reply via email to

[Prev in Thread] Current Thread [Next in Thread]