• 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)
  • From B. Pym@21:1/5 to Matthew Danish on Fri Aug 22 06:36:29 2025
    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

    (use gauche.sequence)

    (define (silly str :optional (inc 1) (ender #f))
    (do ((s str (subseq s 1))
    (n 0 (+ n inc))
    (bag '() (cons (~ s 0) bag))
    (sum 0 (+ n sum)))
    ((eq? ender (ref s 0 ender))
    (values (reverse bag) sum n))))

    (silly "catch" 37 #\h)

    (#\c #\a #\t #\c)
    222
    148


    --
    [T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
    --- Kenny Tilton
    The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham

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