From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stephen Berman Newsgroups: gmane.emacs.help Subject: Re: Program problem Date: Wed, 29 Nov 2023 17:20:30 +0100 Message-ID: <87o7fctttd.fsf@rub.de> References: <446414867.4957940.1701217006858.ref@mail.yahoo.com> <446414867.4957940.1701217006858@mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1949"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Lewis Creary To: Lewis Creary via Users list for the GNU Emacs text editor Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Nov 29 17:21:45 2023 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1r8NJo-0000Em-9g for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Nov 2023 17:21:44 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r8NJ6-0006lu-I9; Wed, 29 Nov 2023 11:21:00 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r8NIn-0006jh-T5 for help-gnu-emacs@gnu.org; Wed, 29 Nov 2023 11:20:45 -0500 Original-Received: from mout.gmx.net ([212.227.17.22]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r8NIk-0007Ql-LP for help-gnu-emacs@gnu.org; Wed, 29 Nov 2023 11:20:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=s31663417; t=1701274831; x=1701879631; i=stephen.berman@gmx.net; bh=pyEiqxnsnK8zWlfcMv+GDT1NlP1QU+oibXhtqxjNqQ4=; h=X-UI-Sender-Class:From:To:Cc:Subject:In-Reply-To:References: Date; b=NxgmX2B7+VU4SERgpS0aoj6Dyeou+bKCzdfXW4D4O/jspWV1//POAtzUYeY7BJKf lDG7mhI6zksR8FPQTuNRbqSZuG4mJQofIbPJ/SuRGqkeNkPTSK/jqOC/+nvt3RbRm 8y5ueTXglLAovz2HGEcQn+wX2rteIi6lDb7yKgM2azDzbTxqFKdD+vyA6lcjRg9no 15uvI3tLkVQSr/LrRByPfbErmKusVU4sRfZ84n8dJOHjVk4jEDc5ObvxmtbrffDJu +JbLTJ02FSgu7fveCHwj0fYv7dznWx9d7uUN2unjg6uPqzdbpavIgrs53lzIuESFw RO1+7tVN0kPn9YsLvw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Original-Received: from strobelfs2 ([88.130.50.245]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1M1Hdq-1rA2Fx0roQ-002pWU; Wed, 29 Nov 2023 17:20:31 +0100 In-Reply-To: <446414867.4957940.1701217006858@mail.yahoo.com> (Lewis Creary via Users list for the's message of "Wed, 29 Nov 2023 00:16:46 +0000 (UTC)") X-Provags-ID: V03:K1:ytQeB9uQgVNnuu3dPu4r9CttfL4lU6zv2xDZ79gqaeZDLoUEy8+ ipKBy5B2oxREm4z5E0k1esSVKO1Ot/kCcippWMH504vhby0g/3qzLtLEBSUmekj67zL28zH PtI2zwnFQhrUuoNPwDAcgQje3z5BKtCZvE0grCctzcoc8PUE80TGi9BsNEJqByRRm7Qtxyq sYCKyhn2BKVZ1nbb/jzqw== UI-OutboundReport: notjunk:1;M01:P0:9Gu4dBP28BI=;cKs5g2WizU2GB2LC0m/L/bDvYcI eCqL0H0FSsobj94D3cAiT0AKVTSsqpSAbAXMkyqEwc7QpKxk5wUHjLY6YWPAGCDiM0G3q8+7i lQBnbj+R5+7D2Hyh6Wysl13NFU7Ou36+PDtrX9x64s2o6tVNfo4cOZMrS1LoQ6kw1DTcX/0G0 tFkSzkWB0Nw5Twrtoz8E4gVEZl8Wm76qb4J9Ku7rCQ9UGqeI6DQtR0ioXiy4cAwWabf1jm40X dCDoyuSV8y9QVf77w6nQBvBcIlKArosrd0qKQVJdHIYr+dTNRyRf9vwMlVo7LxuonaqXjLh4s 35eG0XXnMTPFL/j11UZ2cManqY9LQ5edxEwL81Lh1mPG2cKdhdE+LRwGfSP32BwlWXMrdGi5+ mCE7PsqfUOhUb+yzZniRJ5+snrwtJOQZd0OUh/A84MiBDMrjoaDV9GNa9fHLb6Pt4yUt6m1pj vDFx/X96sULF42KPD3wKLItJtSiBVmho7OrsjTMt45UtMe4zxEDQhWWnYRBIVJoNfE44NhPUw 7JfP/bGt9Shw2dc2xPoN1s1zYL94rRD6/fu5m0bldsvcAXkE9iAIgc5gMrUSWdDiLZpfNG9aI iJwTofDDbIyxYpPSvQN+qbopZ67cwLvdRcSRaghQ+Eb72BNEFXU81lFI8UaVJYIRs2iggsB9V rX7ntbgMlNtAjSP/+AEPjTPSXc3hUg8tVKUDAhcYw+cgZW0734Kf2n00Kn+t6UALMY78kmk0i 2jNaaGo4IcLENBV1OLYnYvMB3W23tnoG20o5GOUE6XxQzSWtA8M7Lm7ji9amyZmhs9vKVdqd Received-SPF: pass client-ip=212.227.17.22; envelope-from=stephen.berman@gmx.net; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:145582 Archived-At: On Wed, 29 Nov 2023 00:16:46 +0000 (UTC) Lewis Creary via Users list for th= e GNU Emacs text editor wrote: > (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 lispfunct= ion, > fill-rows, that I've written (shown below).=C2=A0 The main ideaof this fu= nction is > to help solvers of Sudoku puzzles who want to keeptrack of the numbers th= ey've > already entered into a puzzle.=C2=A0 This willhelp them to backtrack when= they > change some of those numbers.=C2=A0 It'simportant to understand the docum= entation > string of this function(included in the function definition) before readi= ng > the code. Your code appeared badly formatted, so I've reformatted it: (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 the result of filling in row m of the argument puzzle form with numbers specified by the mth 9-digit string." (let* ((row-index 0)=20=20=20=20=20=20=20=20=20 (row-num-index 0) (row-str (nth row-index change-strings))=20=20=20=20=20=20=20=20=20 (row-num (nth row-num-index row-nums)) (change-nums nil)=20=20=20=20=20=20=20=20=20 (str-pos 0)) ;; (comment) cycle through the change-strings (while (and (< row-index (length row-nums)) (< str-pos 9)) ;; cycle through row-str, contributing to change-list (while (< str-pos 9) (setq change-nums (append change-nums=20=20=20=20=20=20=20=20=20=20 (list (string-to-number=20=20=20=20=20=20=20=20=20=20= =20=20=20=20 (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)) There are a number of problems here. One is that you increment str-pos in the inner while-loop but don't reset it after leaving that loop, so that when you start the second run through the outer loop, the condition (< str-pos 9) is false and the function returns pzl-form after only one run. And you keep resetting row-str in the inner loop to the same element of change-strings but don't change it at the end, so you never get to the other strings in change-strings. Another problem is you are changing the same element of pzl-form each time through the outer loop, because rownum doesn't change. Also, in the setcar sexp, the first argument is changing just the first element of the list because you take its car, and the second argument changes it each time to the next element in change-nums. > I'll very much appreciate receiving any proposed bugfixes or other=C2=A0 > suggestions you may have. Here is a variant of your code that returns the result you want; rather the an outer while-loop it uses dolist to loop directly over the row-nums. I've commented out unneeded parts of your original code: (defun fill-rows (row-nums change-strings pzl-form) "" (let ((row-index 0)=20=20=20=20=20=20=20=20=20 ;; (row-num-index 0) ;; (row-str (nth row-index change-strings))=20=20=20=20=20=20=20=20= =20 ;; (row-num (nth row-num-index row-nums)) (row-str "") (change-nums nil)=20=20=20=20=20=20=20=20=20 (str-pos 0)) ;; (comment) cycle through the change-strings (dolist (i row-nums) (setq row-str (nth row-index change-strings)) ;; cycle through row-str, contributing to change-list (while (< str-pos 9) (setq change-nums (append change-nums=20=20=20=20=20=20=20=20=20=20 (list (string-to-number=20=20=20=20=20=20=20=20=20=20= =20=20=20=20 (substring row-str str-pos (1+ str-pos)))))) (cl-incf str-pos)) (setcar (nthcdr i pzl-form) change-nums) (setq str-pos 0 change-nums nil) (cl-incf row-index) ;; (cl-incf row-num-index) ) pzl-form)) As food for thought and for you to experiment with, here is an alternative implementation that uses some higher-level elisp functions to avoid lower-level functions like setcar and nthcdr, and hence shorten the code. Also, it restructures the data into a form that seems to me better suited to what you want, given your description. The doc string tries to explain that: (defun fill-rows (dim rows) "Fill a square matrix of dimensions DIM with values from ROWS. DIM is an integer specifying the number of rows and columns in the matrix. ROWS is an alist with elements (ROW . ELEMENTS), where ROW specifies the (1-based) row number of the matrix and ELEMENTS is a string whose characters are numerals specifying the contents of the cells of ROW (in which each numeral in ELEMENTS has been converted to the corresponding integer). Each cell of the rows not specified by ROWS is filled with 0. Since the result should be a square matrix, the lengths of ROWS and ELEMENTS must equal DIM and each ROW must be an integer between 1 and DIM (inclusive)." (let (matrix) (dotimes (i dim) (let* ((rpair (assq (1+ i) rows)) (row (if rpair (mapcar #'string-to-number (mapcar #'string (string-to-list (cdr rpair)))) (make-list dim 0)))) (setq matrix (append matrix (list row))))) matrix)) Here's the invocation with your test data: (fill-rows 9 '((1 . "123456789") (3 . "987654321") (5 . "123498765"))) I hope that was helpful. Steve Berman