Re: case and quoted keys - a misunderstanding

Liste des GroupesRevenir à cl lisp 
Sujet : Re: case and quoted keys - a misunderstanding
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 23. Jun 2025, 22:04:59
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103cfhq$1gpgh$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:

  (defun read-to-char (c stream)
    (loop for x = (read-char stream nil)
       while x do (when (char= x c) (return t))))
 
How verbose!!  The function can be expressed much more concisely as:
 
     (defun read-to-char (c stream)
       (loop for x = (read-char stream nil)
          while x when (char= x c) return t))
 
or if one doesn't mind the possibility of an extra return value, as:
 
     (defun read-to-char (c stream)
       (ignore-errors (loop when (eql (read-char stream) c) return it))
 
Why is LOOP needed?
 
Scheme:
 
(define (read-to-char c port)
  (let ((r (read-char port)))
    (unless (or (eof-object? r) (eq? c r))
      (read-to-char c port))))

Gauche Scheme

(use srfi-121) ; generators

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


Given:

(define is
  (case-lambda
    [(x) (lambda(y) (equal? y x))]
    [(pred x) (lambda(y) (pred y x))]
    [(pred key x) (lambda(y) (pred (key y) x))]))
 


Date Sujet#  Auteur
16 Jun 25 * Re: case and quoted keys - a misunderstanding2B. Pym
23 Jun 25 `- Re: case and quoted keys - a misunderstanding1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal