• Re: Python syntax in Lisp and Scheme

    From B. Pym@21:1/5 to Frode Vatvedt Fjeld on Sat Jun 29 04:25:59 2024
    Frode Vatvedt Fjeld wrote:

    Scheme
    (define vector-fill!
    (lambda (v x)
    (let ((n (vector-length v)))
    (do ((i 0 (+ i 1)))
    ((= i n))
    (vector-set! v i x)))))

    Python
    def vector_fill(v, x):
    for i in range(len(v)):
    v[i] = x

    To me the Python code is easier to read, and I can't possibly fathom
    how somebody could think the Scheme code is easier to read. It truly boggles my mind. [..]

    The scheme example can only have been written by someone who is on the
    outset determined to demonstrate that sexp-syntax is complicated. This
    is how I'd write it in Common Lisp:

    (defun vector-fill (v x)
    (dotimes (i (length v))
    (setf (aref v i) x)))

    As you can see, it matches the python example quite closely.

    Why would any human want to match Python?

    ;; Racket
    (define (vec-fill! v x)
    (vector-map! (const x) v))

    ;; Gauche Scheme
    (define (vec-fill! v x)
    (vector-map! (lambda _ x) v))

    (define vec (vector 2 3 4))

    (vec-fill! vec 88)

    vec
    ===>
    #(88 88 88)

    However, vector-fill! is already provided.

    (vector-fill! vec 99)

    vec
    ===>
    #(99 99 99)

    Multiply each element by 2:

    (vector-map! (lambda(n) (* 2 n)) vec)

    vec
    ===>
    #(198 198 198)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Pascal Costanza on Fri Sep 13 19:22:53 2024
    XPost: comp.lang.scheme

    Pascal Costanza wrote:

    What about dealing with an arbitrary number of filters?

    (defmacro predicate-collect (list &body predicates)
    (let ((collectors (mapcar (lambda (predicate)
    (declare (ignore predicate))
    (gensym "COLLECT"))
    predicates))
    (collect-t (gensym "COLLECT")))
    `(with-collectors (,@collectors ,collect-t)
    (dolist (l ,list)
    (cond ,@(mapcar (lambda (predicate collector)
    `((funcall ,predicate l) (,collector l)))
    predicates collectors)
    (t (,collect-t l)))))))

    An example:

    (predicate-collect '(-5 -4 -3 -2 -1 0 1 2 3 4 5)
    (function evenp)
    (lambda (n) (< n 0))
    (lambda (n) (> n 3)))
    (-4 -2 0 2 4)
    (-5 -3 -1)
    (5)
    (1 3)


    I use the list collector macros by Tim Bradshaw here - see http://www.tfeb.org/lisp/hax.html#COLLECTING

    Gauche Scheme

    (define (multi-partition the-list . predicates)
    (if (null? predicates)
    (list the-list)
    (receive (yes no)
    (partition (car predicates) the-list)
    (cons yes (apply multi-partition no (cdr predicates))))))

    (multi-partition '(-5 -4 -3 -2 -1 0 1 2 3 4 5)
    even?
    (is < 0)
    (is > 3))

    ((-4 -2 0 2 4) (-5 -3 -1) (5) (1 3))

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Paul Rubin on Fri Sep 13 20:38:02 2024
    Paul Rubin wrote:

    Scheme
    (define vector-fill!
    (lambda (v x)
    (let ((n (vector-length v)))
    (do ((i 0 (+ i 1)))
    ((= i n))
    (vector-set! v i x)))))

    I think you could write the scheme code like this:

    (define vector-fill! (v x)
    (let ((i 0))
    (while (< i (length v))
    (vector-set! v i x)
    (set! i (1+ i)))))

    Gauche Scheme

    (define myvec (vector 0 0 0 0 0))
    (vector-fill! myvec 99)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Frode Vatvedt Fjeld on Wed Jul 9 15:51:45 2025
    XPost: comp.lang.scheme

    Frode Vatvedt Fjeld wrote:

    Scheme
    (define vector-fill!
    (lambda (v x)
    (let ((n (vector-length v)))
    (do ((i 0 (+ i 1)))
    ((= i n))
    (vector-set! v i x)))))

    Python
    def vector_fill(v, x):
    for i in range(len(v)):
    v[i] = x

    To me the Python code is easier to read, and I can't possibly fathom
    how somebody could think the Scheme code is easier to read. It truly boggles my mind. [..]

    The scheme example can only have been written by someone who is on the
    outset determined to demonstrate that sexp-syntax is complicated. This
    is how I'd write it in Common Lisp:

    (defun vector-fill (v x)
    (dotimes (i (length v))
    (setf (aref v i) x)))

    As you can see, it matches the python example quite closely.

    Why would any human want to match Python?

    Gauche Scheme

    (vector-fill! vec 99)

    (vector-map! (constantly 88) vec)


    Multiply each element by 2:

    (vector-map! (cut * 2 <>) vec)

    Sum the elements in the vector.

    (use scheme.vector)
    (vector-fold + 0 vec)

    Another way to sum.

    (use gauche.sequence)
    (fold + 0 vec)

    Make a list containing the positive numbers in the vector.

    (use gauche.sequence)
    (filter positive? vec)

    Find location of first negative number.

    (use scheme.vector)
    (vector-index negative? vec)

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