From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: lloda Newsgroups: gmane.lisp.guile.user Subject: Re: vector-last / vector-ref with negative indices? Date: Sun, 20 Dec 2020 11:41:23 +0100 Message-ID: References: Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) 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="22092"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user To: Christopher Lam Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Dec 20 11:41:50 2020 Return-path: Envelope-to: guile-user@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 1kqw9u-0005dp-3z for guile-user@m.gmane-mx.org; Sun, 20 Dec 2020 11:41:50 +0100 Original-Received: from localhost ([::1]:58370 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kqw9s-0006UA-Va for guile-user@m.gmane-mx.org; Sun, 20 Dec 2020 05:41:48 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48802) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kqw9f-0006Tq-Ru for guile-user@gnu.org; Sun, 20 Dec 2020 05:41:35 -0500 Original-Received: from mta-11-4.privateemail.com ([198.54.127.104]:2064) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kqw9d-0008FZ-Qm for guile-user@gnu.org; Sun, 20 Dec 2020 05:41:35 -0500 Original-Received: from mta-11.privateemail.com (localhost [127.0.0.1]) by mta-11.privateemail.com (Postfix) with ESMTP id C3EA88004A; Sun, 20 Dec 2020 05:41:25 -0500 (EST) Original-Received: from [192.168.1.105] (unknown [10.20.151.232]) by mta-11.privateemail.com (Postfix) with ESMTPA id 1804680041; Sun, 20 Dec 2020 10:41:24 +0000 (UTC) In-Reply-To: X-Mailer: Apple Mail (2.3608.120.23.2.4) X-Virus-Scanned: ClamAV using ClamSMTP Received-SPF: pass client-ip=198.54.127.104; envelope-from=lloda@sarc.name; helo=MTA-11-4.privateemail.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17092 Archived-At: Besides the standard which applies to vector- operations, negative = indices wouldn't work for Guile arrays to count from the end since = negative indices, like in say Fortran, can be valid [e.g. (make-array 0 = '(-1 4))]. In older versions of Guile you could use vector-ref on such = arrays. I don't think allowing negative indices is worthwhile for all the = complications it introduces. But I don't like negative indices to count = from the end either. Most of the time vectors aren't meant to have that = wraparound property so it can turn a obvious error into an obscure one. The solution I like best is to have a keyword that means 'the end', like = in Octave, that can be used generically. Barring that you can easily = define vector-last or even something like vector-ref-from-end I guess. I = think it's weird that srfi-43 doesn't have vector-last, when srfi-1 has = last. Maybe Guile could add that as an extension, although I don't like = srfi-43 in general (I think the map functions are wrongly designed). regards Daniel > On 20 Dec 2020, at 11:05, Christopher Lam = wrote: >=20 > Easy - vector-ref with a negative index is not defined in r[5|7]rs. >=20 > On Sun, 20 Dec 2020 at 06:59, Aleix Conchillo Flaqu=C3=A9 = > wrote: >=20 >> Hi, >>=20 >> This month I'm trying to go through Advent Of Code one more year >> (previous years I didn't get too far) and I've been finding myself >> writing the same patterns multiple times that could be avoided by = just >> having a helper function. >>=20 >> One of them is getting the last element of a vector. It is a quite >> common operation when solving these types of problems. For example >> Python as you might know uses negative indices. >>=20 >> I have looked around and haven't seen it (unless I completely missed >> it which would be shameful) but wouldn't it be good to have this >> built-in by default? >>=20 >> Instead of having to write: >>=20 >> (vector-ref v (- (vector-length v) 1)) >>=20 >> you would write: >>=20 >> (vector-last v) or even better (vector-ref v -1). >>=20 >> Interestingly Racket doesn't offer those functions either as far as I = can >> tell. >>=20 >> Basic use case? Get the maximum element of a sorted vector which = would >> be done in constant time. >>=20 >> I'm trying to solve the problems idiomatically, to the best of my >> limited knowledge, with Scheme. It is possible that the times I'm >> using vectors I should think about it differently and not use them, >> but I'm not 100% sure. >>=20 >> What am I missing? >>=20 >> Thank you in advance, >>=20 >> Aleix >>=20 >>=20