• Re: DEFUN list argument

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Fri Jul 11 05:00:22 2025
    From Newsgroup: comp.lang.scheme

    Steven E. Harris wrote:

    (defun dot-product (u v)
    (loop for elem-u across u
    for elem-v across v
    summing (* elem-u elem-v)))


    (dot-product (vector 1 2 3)
    (vector 4 5 6))
    32

    Gauche Scheme

    (use gauche.sequence)

    (define (dot-product u v)
    (fold
    (lambda (e-u e-v sum) (+ sum (* e-u e-v)))
    0
    u
    v))

    (dot-product #(1 2 3) #(4 5 6))
    ===>
    32


    Here's a version that handles any number of vectors.

    (use scheme.vector)

    (define (dot-product . vecs)
    (apply vector-fold
    (lambda (sum . elems) (+ sum (apply * elems)))
    0
    vecs))

    (dot-product #(1 2 3) #(4 5 6) #(7 8 9))
    ===>
    270



    Shorter:

    (define (dot-product u v)
    (fold + 0 (map * u v)))


    Shorter:

    (define (dot-product u v)
    (apply + (map * u v)))
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Wed Aug 6 00:00:09 2025
    From Newsgroup: comp.lang.scheme

    B. Pym wrote:

    Steven E. Harris wrote:

    (defun dot-product (u v)
    (loop for elem-u across u
    for elem-v across v
    summing (* elem-u elem-v)))


    (dot-product (vector 1 2 3)
    (vector 4 5 6))
    32

    Gauche Scheme

    (define (dot-product U V)
    (gmk u U)
    (gmk v V)
    (do ((s 0 (+ s (* (u) (v)))))
    ((unull?) s)))

    (dot-product (vector 1 2 3) (vector 4 5 6))
    ===>
    32

    Given:

    ;; Make a generator.
    (define (gmk* vec)
    (let ((v vec)
    (i 0))
    (values
    (lambda()
    (if (= i (vector-length v)) #f (begin0 (~ v i) (inc! i))))
    (lambda() (= i (vector-length v))))))

    (define-macro gmk
    (lambda (sym vec)
    `(define-values
    (,sym ,(symbol-append sym 'null?))
    (gmk* ,vec))))
    --
    [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 --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Wed Aug 6 01:10:42 2025
    From Newsgroup: comp.lang.scheme

    B. Pym wrote:

    B. Pym wrote:

    Steven E. Harris wrote:

    (defun dot-product (u v)
    (loop for elem-u across u
    for elem-v across v
    summing (* elem-u elem-v)))


    (dot-product (vector 1 2 3)
    (vector 4 5 6))
    32

    Gauche Scheme

    (define (dot-product U V)
    (gmk u U)
    (gmk v V)
    (do ((s 0 (+ s (* (u) (v)))))
    ((unull?) s)))

    (dot-product (vector 1 2 3) (vector 4 5 6))
    ===>
    32

    Given:

    ;; Make a generator.
    (define (gmk* vec)
    (let ((v vec)
    (i 0))
    (values
    (lambda()
    (if (= i (vector-length v)) #f (begin0 (~ v i) (inc! i))))
    (lambda() (= i (vector-length v))))))

    (define-macro gmk
    (lambda (sym vec)
    `(define-values
    (,sym ,(symbol-append sym 'null?))
    (gmk* ,vec))))

    Shorter:

    (define (dot-product U V)
    (gmk u U v V)
    (do ((s 0 (+ s (* (u) (v)))))
    ((unull?) s)))

    (define-macro gmk
    (lambda args
    (define duos
    (let go ()
    (if (null? args) '() (cons (list (pop! args)(pop! args)) (go)))))
    (define (def s v)
    `(define-values
    (,s ,(symbol-append s 'null?))
    (gmk* ,v)))
    `(begin ,@(map (lambda (xs) (apply def xs)) duos))))
    --
    [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 --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Thu Aug 28 06:15:49 2025
    From Newsgroup: comp.lang.scheme

    B. Pym wrote:

    Steven E. Harris wrote:

    (defun dot-product (u v)
    (loop for elem-u across u
    for elem-v across v
    summing (* elem-u elem-v)))


    (dot-product (vector 1 2 3)
    (vector 4 5 6))
    32

    Gauche Scheme

    (use gauche.sequence)

    (define (dot-product u v)
    (fold
    (lambda (e-u e-v sum) (+ sum (* e-u e-v)))
    0
    u
    v))

    (dot-product #(1 2 3) #(4 5 6))
    ===>
    32

    Gauche Scheme

    (use gauche.sequence) ;; So that "fold" can handle vectors.

    (define (dot-product u v)
    (% fold (+ (* A: B:) C:) 0 u v))

    (dot-product (vector 1 2 3) (vector 4 5 6))
    ===>
    32

    Given:

    ;; Anaphoric macro to abreviate lambdas for higher-order functions.
    ;; Uses "_" for 1 argument;
    ;; uses "A:" and "B:" and so on for 2 or more arguments.
    ;; This version works under both Gauche Scheme
    ;; and Racket. Racket needs:
    ;; (require compatibility/defmacro)
    ;;
    (define-macro %
    (case-lambda
    ((func expr List) `(,func (lambda(_) ,expr) ,List))
    ((func expr . more)
    (let* ((n 64)
    (nums (map (lambda _ (set! n (+ 1 n)) n) more))
    (vnames
    (map (lambda(n)
    (string->symbol (string (integer->char n) #\:)))
    nums)))
    `(,func (lambda ,vnames ,expr) ,@more)))))
    --
    [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 --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Sun Sep 29 00:52:08 2024
    From Newsgroup: comp.lang.scheme

    (defun list-of-length (n list)
    "Predicate which tests whether LIST is a list
    of length N."
    (loop for i from 0 below n
    when (null list) do (return nil)
    do (pop list)
    finally (return (null list))))

    Instead of using a macro whose source measures 60 kilobytes,
    we can make it shorter by simply using recursion. Of course,
    that's not possible in CL since it's not a Lispy language and
    doesn't offer tail-call optimization. (Lispy languages
    encourage recursive solutions.)

    Scheme:

    (define (list-of-length? n lst)
    (cond ((zero? n) (null? lst))
    ((null? lst) #f)
    (#t (list-of-length? (- n 1) (cdr lst)))))
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Kaz Kylheku@643-408-1753@kylheku.com to comp.lang.lisp,comp.lang.scheme on Sun Sep 29 02:21:46 2024
    From Newsgroup: comp.lang.scheme

    On 2024-09-29, B. Pym <Nobody447095@here-nor-there.org> wrote:
    (defun list-of-length (n list)
    "Predicate which tests whether LIST is a list
    of length N."
    (loop for i from 0 below n
    when (null list) do (return nil)
    do (pop list)
    finally (return (null list))))

    Instead of using a macro whose source measures 60 kilobytes,

    ... we can use a macro whose source is 20 lines and which is not
    included in our languager implementation, and then claim that a 7 line
    function that must be accompanied by that macro is shorter than a 5 line function depending only on ANSI Common Lisp.

    we can make it shorter by simply using recursion. Of course,
    that's not possible in CL since it's not a Lispy language and
    doesn't offer tail-call optimization.

    The Common Lisp specification doesn't require it, but implementations
    offer it.

    The ISO C specification deosn't require any optimization at all; yet C
    users rely on optimizations (one of them being TCO, by the way).

    In C, nobody in their right mind writes i >> 1 rather than i / 2 any
    more, even though ISO C doesn't "offer" the assumption that i / 2 will
    be reduced to a shift.

    Scheme:

    Scheme offers very little; most serious work done with Scheme picks
    an implementation and uses it.

    Many of your own posts pick a particular Scheme like Gauche and
    often use its extensions that are not in Scheme.

    (define (list-of-length? n lst)
    (cond ((zero? n) (null? lst))
    ((null? lst) #f)
    (#t (list-of-length? (- n 1) (cdr lst)))))

    You would avoid this kind of function entirely (and linked lists
    all together) in code that is to be maximally performant. Linked
    lists have poor cache performance. Chasing down a linked list
    involved dependent loads which the machine cannot prefetch, unlike
    marching down an array that is contiguously laid out in memory.

    The battle you are trying to pick here is outdated.

    Whether list processing code is recursive or iterative, and how fast it
    is, is largerly irrelevant. Most list processing is compile time code
    wrangling (macros and whatnot). Performance of the compile time is
    not highly relevant, or else nobody would be using C++.
    --
    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
  • From Axel Reichert@mail@axel-reichert.de to comp.lang.lisp,comp.lang.scheme on Sun Sep 29 13:51:54 2024
    From Newsgroup: comp.lang.scheme

    Kaz Kylheku <643-408-1753@kylheku.com> writes:

    Scheme offers very little; most serious work done with Scheme picks an implementation and uses it.

    Many of your own posts pick a particular Scheme like Gauche and often
    use its extensions that are not in Scheme.

    Like in geography: "Where does London end?" (-:

    Linked lists have poor cache performance. Chasing down a linked list
    involved dependent loads which the machine cannot prefetch, unlike
    marching down an array that is contiguously laid out in memory.

    The battle you are trying to pick here is outdated.

    I have read so a couple of times. Interesting. But what is a Lisper to
    do in the source code? Convert it all to vectors/arrays? Use more
    imperative idioms than recursion? Do not care, because it is all handled
    by the implementation?

    Honestly curious,

    Axel
    --- Synchronet 3.22a-Linux NewsLink 1.2