From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Christopher Lam Newsgroups: gmane.lisp.guile.user Subject: Re: vector-last / vector-ref with negative indices? Date: Sun, 20 Dec 2020 10:05:51 +0000 Message-ID: References: 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="38043"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user To: =?UTF-8?Q?Aleix_Conchillo_Flaqu=C3=A9?= Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Dec 20 11:06:34 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 1kqvbm-0009ol-4z for guile-user@m.gmane-mx.org; Sun, 20 Dec 2020 11:06:34 +0100 Original-Received: from localhost ([::1]:49852 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kqvbl-0000ci-4J for guile-user@m.gmane-mx.org; Sun, 20 Dec 2020 05:06:33 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44864) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kqvba-0000by-Fw for guile-user@gnu.org; Sun, 20 Dec 2020 05:06:22 -0500 Original-Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]:34686) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kqvbY-0007e3-Nf for guile-user@gnu.org; Sun, 20 Dec 2020 05:06:22 -0500 Original-Received: by mail-pl1-x632.google.com with SMTP id t6so4064478plq.1 for ; Sun, 20 Dec 2020 02:06:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=QAlrY1e+1MKo6Gdg63fNmG8bNn0XTDC8IAS7/saN2Bg=; b=Av9CA6IUs4WqtvzLwz+8gp7wHQnyA6uvhJ86r50Wo9TfkUL8x8Cyfuroj3FKmzyAMf nL2n2jrpD+PkznC50Ny2RH9yqt6cigrgX+W44qSgRiFlgQoKjk87lu0FKgLRc+B5ZJAS cWgSJiOO+sPgMSPdI0bsbk56RdBRyDh5m6GPoXbt4pzmjO5n0RQwnI/g10arsczyAQcT LuzqWUnA3AliuPmAOrst2JWA56nX6FqIDdDPw56WL8lu4TrtgcICXAyIUgCWaheQ5StW 30mh1+vPNE89xaLdrWFTvRJGsfZ6z9KVxYH+0BQxgrwVWuOfohYWHsij+1SsYZnCgrVh WJ4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=QAlrY1e+1MKo6Gdg63fNmG8bNn0XTDC8IAS7/saN2Bg=; b=saaGynhnlzvioeOoLxqwV3sUJFoffvXfxJca+EC74+oZCWBkrbZaz/4obis1s9EgrO YL0YN/6s6BMT1HCZhipbuATEqmLB6zsXFumO1bDkSe/CFeRPdIhTPv9wNaXmmfL2cQQi h3o/GGtbkZFrQlYPGtz8IUEi5QbrBho34j/4HYZr4Nqme+Qsn7kdusKOVe6vcVeNckqa Xt68Du/aC3A5FUjrfZji8IiAmYR/rq9oGkciPvbhzS7fqfyStpsa84C0C2LU6fDgWr/d Hzce/dwzcFCKhuOh2wE3JpHFhV72KgVxbWBrhHARX+od7CCNZYCvJNPGLiRTJOch3RQS HU6Q== X-Gm-Message-State: AOAM533qdNPr+9gRdLp4aJw8wQ4StcM04se3hDBsh2efElHd8zEBIAoc cxhVyZ8lFfOCjKrV/p9fGaxg3nLhymu7Adid5do= X-Google-Smtp-Source: ABdhPJwtexB5bRlJUXoOC9/Cohg96JX2k04t34KfrWQ7UtOQPXb7bFApAPlhAHOLVdDGqaE9I6Ut7gRns3SF35SH4vs= X-Received: by 2002:a17:902:b90c:b029:db:f23d:d684 with SMTP id bf12-20020a170902b90cb02900dbf23dd684mr11845936plb.43.1608458778703; Sun, 20 Dec 2020 02:06:18 -0800 (PST) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=christopher.lck@gmail.com; helo=mail-pl1-x632.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 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:17091 Archived-At: Easy - vector-ref with a negative index is not defined in r[5|7]rs. On Sun, 20 Dec 2020 at 06:59, Aleix Conchillo Flaqu=C3=A9 wrote: > Hi, > > 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. > > 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. > > 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? > > Instead of having to write: > > (vector-ref v (- (vector-length v) 1)) > > you would write: > > (vector-last v) or even better (vector-ref v -1). > > Interestingly Racket doesn't offer those functions either as far as I can > tell. > > Basic use case? Get the maximum element of a sorted vector which would > be done in constant time. > > 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. > > What am I missing? > > Thank you in advance, > > Aleix > >