From: Stephen Berman <stephen.berman@gmx.net>
To: Stephen Berman <stephen.berman@gmx.net>
Cc: Lewis Creary via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org>, Lewis Creary <lewcreary@cs.com>
Subject: Re: Program problem
Date: Wed, 29 Nov 2023 17:37:31 +0100 [thread overview]
Message-ID: <87jzq0tt10.fsf@rub.de> (raw)
In-Reply-To: <87o7fctttd.fsf@rub.de> (Stephen Berman's message of "Wed, 29 Nov 2023 17:20:30 +0100")
On Wed, 29 Nov 2023 17:20:30 +0100 Stephen Berman <stephen.berman@gmx.net> wrote:
[...]
> 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:
I meant to add here that the last sentence of the doc string suggests
how to check for bad input, but I omitted adding such code for the sake
of brevity.
Steve Berman
> (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
prev parent reply other threads:[~2023-11-29 16:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <446414867.4957940.1701217006858.ref@mail.yahoo.com>
2023-11-29 0:16 ` Program problem Lewis Creary via Users list for the GNU Emacs text editor
2023-11-29 5:31 ` Emanuel Berg
2023-11-29 15:00 ` Manuel Giraud via Users list for the GNU Emacs text editor
2023-11-29 16:20 ` Stephen Berman
2023-11-29 16:37 ` Stephen Berman [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87jzq0tt10.fsf@rub.de \
--to=stephen.berman@gmx.net \
--cc=help-gnu-emacs@gnu.org \
--cc=lewcreary@cs.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.