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 comp.lang.scheme
Date : 14. Sep 2024, 00:59:51
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vc2g56$12ukb$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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))]))

Date Sujet#  Auteur
14 Sep00:59 * Re: case and quoted keys - a misunderstanding2B. Pym
14 Sep18:38 `- Re: case and quoted keys - a misunderstanding1Kaz Kylheku

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal