Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You are destined to become the commandant of the fighting men of the department of transportation.


comp / comp.lang.lisp / Re: case and quoted keys - a misunderstanding

SubjectAuthor
* Re: case and quoted keys - a misunderstandingB. Pym
`- Re: case and quoted keys - a misunderstandingKaz Kylheku

1
Subject: Re: case and quoted keys - a misunderstanding
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 13 Sep 2024 22:59 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: case and quoted keys - a misunderstanding
Date: Fri, 13 Sep 2024 22:59:51 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <vc2g56$12ukb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 14 Sep 2024 00:59:51 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ee687035ab0f3367b9efcf5ae7f07364";
logging-data="1145483"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GfRJDu21mTFYu5p9RdW3S"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:ppZGK3ndHsIrmti1mS8eOFYQohM=
View all headers

Frode Vatvedt Fjeld wrote:

> > (defun read-to-char (c stream)
> > (let ((x (read-char stream nil)))
> > (cond ((null x) nil)
> > ((char= x c) t)
> > (t (read-to-char c stream)))))
> >
> > This isn't much worse looking then the obvious iterative solutions.
>
> ..only it has Scheme written all over it ;-) In Common Lisp this is
> spelled as
>
> (defun read-to-char (c stream)
> (loop for x = (read-char stream nil)
> while x do (when (char= x c) (return t))))

It's shorter in Gauche Scheme:

(use srfi-42) ;; first-ec

(define (read-to-char c port)
(first-ec #f
(:port x port read-char)
(if (char=? x c))
#t))

Testing.

(call-with-input-string "foo-Frodo is foolish."
(lambda (in) (read-to-char #\- in)
(read-line in)))
===>
"Frodo is foolish."

Shorter yet:

(use srfi-121) ; generators

(define (read-to-char c port)
(generator-find (is c) (cut read-char port)))

Given:

(define-syntax is
(syntax-rules ()
[(is x)
(lambda (y) (equal? y x))]
[(is compare x)
(lambda (y) (compare y x))]
[(is key compare x)
(lambda (y) (compare (key y) x))]))

Subject: Re: case and quoted keys - a misunderstanding
From: Kaz Kylheku
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sat, 14 Sep 2024 16:38 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: case and quoted keys - a misunderstanding
Date: Sat, 14 Sep 2024 16:38:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <20240913182544.67@kylheku.com>
References: <vc2g56$12ukb$1@dont-email.me>
Injection-Date: Sat, 14 Sep 2024 18:38:54 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="70215a4d804215b5dfff445270e300c7";
logging-data="1658795"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19XH7my1w0vftVH/jzWa3/CPf4uY8NM5A0="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:V7vv75Mjt5G4AQp5l9QkBRHtztQ=
View all headers

On 2024-09-13, B. Pym <Nobody447095@here-nor-there.org> wrote:
> Frode Vatvedt Fjeld wrote:
>
>> > (defun read-to-char (c stream)
>> > (let ((x (read-char stream nil)))
>> > (cond ((null x) nil)
>> > ((char= x c) t)
>> > (t (read-to-char c stream)))))
>> >
>> > This isn't much worse looking then the obvious iterative solutions.
>>
>> ..only it has Scheme written all over it ;-) In Common Lisp this is
>> spelled as
>>
>> (defun read-to-char (c stream)
>> (loop for x = (read-char stream nil)
>> while x do (when (char= x c) (return t))))
>
> It's shorter in Gauche Scheme:
>
> (use srfi-42) ;; first-ec
>
> (define (read-to-char c port)
> (first-ec #f
> (:port x port read-char)
> (if (char=? x c))
> #t))
>
> Testing.
>
> (call-with-input-string "foo-Frodo is foolish."
> (lambda (in) (read-to-char #\- in)
> (read-line in)))
> ===>
> "Frodo is foolish."
>
> Shorter yet:

What, that is longer?

> (use srfi-121) ; generators
>
> (define (read-to-char c port)
> (generator-find (is c) (cut read-char port)))
>
> Given:
>
> (define-syntax is
> (syntax-rules ()
> [(is x)
> (lambda (y) (equal? y x))]
> [(is compare x)
> (lambda (y) (compare y x))]
> [(is key compare x)
> (lambda (y) (compare (key y) x))]))

The "Given:" has to count as part of the length of the solution.

Otherwiwse, we can just say:

" Shorter yet:
;; nothing

Given:

(define (read-to-char c port) ...) "

If you can factor out any aspect of your solution into macros
and functions which don't count toward its size, then you just
do that with the function you are supposed to write, and
the solution has zero size.

--
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