Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #26: first Saturday after first full moon in Winter


comp / comp.lang.lisp / Re: Another code review perhaps?

SubjectAuthor
* Re: Another code review perhaps?B. Pym
`- Re: Another code review perhaps?Kaz Kylheku

1
Subject: Re: Another code review perhaps?
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Thu, 12 Sep 2024 09:12 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Another code review perhaps?
Date: Thu, 12 Sep 2024 09:12:38 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <vbuba3$68dk$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Thu, 12 Sep 2024 11:12:38 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="0068c9cac97edf80ad7dcfbe07391a5a";
logging-data="205236"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ftnz1bdTV5El6NRvNgwWz"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:M/Fm46qklSFwk+olJJ9Kd6rPW/E=
View all headers

Arthur Lemmens wrote:

> > Define iterative and recursive versions of a function that takes an
> > object x and a vector v, and returns a list of all the objects that
> > immediately precede x in v.
>
> Graham doesn't like LOOP, but I do. So here's a LOOP-version:
>
> (defun precedes (object vector)
> (loop for x across vector
> and i from 0
> when (and (equal x object) (> i 0))
> collect (elt vector (1-i))))

Look at that:

(1-i)

Don't you think that that should be:

(1- i)

or

(- i 1)

?

It's shorter when you use a Lispy language instead of CL.

Gauche Scheme

(use srfi-42) ; list-ec

(define (precedes obj vec)
(list-ec (: x (index i) vec)
(if (and (> i 0) (equal? x obj)))
(ref vec (- i 1))))

(precedes 5 #(5 0 4 5 8 9 5))
===>
(4 9)

Another way:

(define (precedes o v)
(let ((l (vector->list vec)))
(filter-map
(^(a b) (and (equal? o a) b))
(cdr l)
l)))

Subject: Re: Another code review perhaps?
From: Kaz Kylheku
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Thu, 12 Sep 2024 12:22 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Another code review perhaps?
Date: Thu, 12 Sep 2024 12:22:24 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <20240912051355.168@kylheku.com>
References: <vbuba3$68dk$1@dont-email.me>
Injection-Date: Thu, 12 Sep 2024 14:22:24 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="fc240c90c706e031471363b92b2225a2";
logging-data="270113"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/A+ZGG7qbH18lEm7w3yeNr/hXvn9BRdEU="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:hfbmZj0Yy+LRXc9myX03csW3d/E=
View all headers

On 2024-09-12, B. Pym <Nobody447095@here-nor-there.org> wrote:
> Arthur Lemmens wrote:
>
>> > Define iterative and recursive versions of a function that takes an
>> > object x and a vector v, and returns a list of all the objects that
>> > immediately precede x in v.
>>
>> Graham doesn't like LOOP, but I do. So here's a LOOP-version:
>>
>> (defun precedes (object vector)
>> (loop for x across vector
>> and i from 0
>> when (and (equal x object) (> i 0))
>> collect (elt vector (1-i))))
>
>
> Look at that:
>
> (1-i)
>
> Don't you think that that should be:
>
> (1- i)
>
> or
>
> (- i 1)
>
> ?
>
> It's shorter when you use a Lispy language instead of CL.
>
> Gauche Scheme
>
> (use srfi-42) ; list-ec
>
> (define (precedes obj vec)
> (list-ec (: x (index i) vec)
> (if (and (> i 0) (equal? x obj)))
> (ref vec (- i 1))))
>
> (precedes 5 #(5 0 4 5 8 9 5))
> ===>
> (4 9)
>
> Another way:
>
> (define (precedes o v)
> (let ((l (vector->list vec)))
> (filter-map
> (^(a b) (and (equal? o a) b))
> (cdr l)
> l)))

1> (match @(scan-all (@x 5 . @nil)) '(5 0 4 5 8 9 5) x)
(4 9)
2> (window-mappend 1 nil (do if (and @1 (= @2 5)) (list @1)) #(5 0 4 5 8 9 5))
#(4 9)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

1

rocksolid light 0.9.8
clearnet tor