• Re: case and quoted keys - a misunderstanding

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Fri Sep 13 22:59:51 2024
    From Newsgroup: comp.lang.scheme

    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))]))
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Kaz Kylheku@643-408-1753@kylheku.com to comp.lang.lisp,comp.lang.scheme on Sat Sep 14 16:38:54 2024
    From Newsgroup: comp.lang.scheme

    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
    --- Synchronet 3.22a-Linux NewsLink 1.2