• Re: case and quoted keys - a misunderstanding

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Mon Jun 16 06:41:46 2025
    From Newsgroup: comp.lang.lisp

    (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))))
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Mon Jun 23 21:04:59 2025
    From Newsgroup: comp.lang.lisp

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


    --- Synchronet 3.21d-Linux NewsLink 1.2