• Re: macros vs HOFs (was: O'Caml)

    From B. Pym@21:1/5 to Matthew Danish on Sun Sep 15 11:52:03 2024
    XPost: comp.lang.scheme

    Matthew Danish wrote:

    If you want to have some fun, why not write a nice higher-order function
    to do:

    (defun silly-loop (string &optional (increment 1) (final-char nil))
    (loop for n from 0 by increment
    for char across string
    until (eql char final-char)
    collect char into char-bag
    sum n into sum
    finally (return (values char-bag sum n))))

    Try to make it half as readable. And as efficient.

    Gauche Scheme

    (use gauche.sequence) ;; map [generic]

    (define (silly str :optional (incr 1) (ender #f))
    (let@ (0 n sum)
    (values
    (map (^(char) (inc! sum n) (inc! n incr) char)
    (if ender
    (car (string-split str ender))
    str))
    sum n)))

    (silly "catch" 37 #\h)
    ===>
    (#\c #\a #\t #\c)
    222
    148

    (silly "catch")
    ===>
    (#\c #\a #\t #\c #\h)
    10
    5

    Given:

    (define-syntax let@-aux
    (syntax-rules ()
    [(let@-aux (0 var ...) (pairs ...) stuff)
    (let@-aux () (pairs ... (var 0) ...) stuff)]
    [(let@-aux ('() var ...) (pairs ...) stuff)
    (let@-aux () (pairs ... (var '()) ...) stuff)]
    [(let@-aux (var val more ...) (pairs ...) stuff)
    (let@-aux (more ...) (pairs ... (var val)) stuff)]
    [(let@-aux (var) pairs stuff)
    (let@-aux (var '()) pairs stuff)]
    [(let@-aux () ((var val) ...) (stuff ...))
    (let* ((var val) ...) stuff ...)]))
    (define-syntax let@
    (syntax-rules ()
    [(let@ things stuff ...)
    (let@-aux things () (stuff ...))]))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)