• Re: Ex. 3.5 in ACL

    From B. Pym@21:1/5 to Zachary Beane on Fri Sep 13 21:07:41 2024
    XPost: comp.lang.scheme

    Zachary Beane wrote:

    (defun mapcar-pos+ (list)
    (let ((i -1))
    (mapcar #'(lambda (elt) (+ elt (incf i)))

    Truly abysmal ignorance or willful stupidity.

    The #' is redundant.


    iteration:

    (defun pos+ (lst)
    (setf acc NIL)
    (setf i 0)
    (dolist (obj lst)
    ; i know, instead of append, i could do a cons and reverse afterwards...
    (progn (setf acc (append acc (list (+ obj i))))
    (setf i (+ i 1))))
    acc)

    I'd prefer LOOP here:

    (defun loop-pos+ (list)
    (loop for i from 0
    for elt in list
    collect (+ elt i)))

    Gauche Scheme:

    (define (pos+ input)
    (@ (i -1) map x : (+ x (++ i)) input))

    (pos+ '(900 800 700 600 500))
    ===>
    (900 801 702 603 504)

    Given:

    (define ++ inc!)

    (define-syntax @-aux
    (syntax-rules (:)
    [(_ () goodlets func (vars ...) : expr lst ...)
    (let* goodlets (func (lambda (vars ...) expr) lst ...))]
    [(_ () goodlets func (vars ...) var more ...)
    (@-aux () goodlets func (vars ... var) more ...)]
    [(_ (var val more ...) (goodlets ...) stuff ...)
    (@-aux (more ...) (goodlets ... (var val)) stuff ...)]))

    (define-syntax @
    (syntax-rules ()
    [(_ (lets ...) func stuff ...)
    (@-aux (lets ...) () func () stuff ...)]
    [(_ func stuff ...)
    (@ () func stuff ...)]))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Tue Jun 10 08:06:01 2025
    XPost: comp.lang.scheme

    B. Pym wrote:

    Zachary Beane wrote:

    (defun mapcar-pos+ (list)
    (let ((i -1))
    (mapcar #'(lambda (elt) (+ elt (incf i)))

    Truly abysmal ignorance or willful stupidity.

    The #' is redundant.


    iteration:

    (defun pos+ (lst)
    (setf acc NIL)
    (setf i 0)
    (dolist (obj lst)
    ; i know, instead of append, i could do a cons and reverse afterwards...
    (progn (setf acc (append acc (list (+ obj i))))
    (setf i (+ i 1))))
    acc)

    I'd prefer LOOP here:

    (defun loop-pos+ (list)
    (loop for i from 0
    for elt in list
    collect (+ elt i)))

    Gauche Scheme:


    (map + '(200 300 400) (lrange 0))

    (200 301 402)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Zachary Beane on Wed Jul 9 14:45:44 2025
    XPost: comp.lang.scheme

    Zachary Beane wrote:

    I am just trying to solve Ex 3.5 in Graham's ANSI Common Lisp book. I am reading it on my own and not as part of a university course. The task is:

    define a function pos+, that takes a list as param and returns a list
    that adds the position of each element of the list to the element's value. Thus:
    (pos+ '(7 5 1 4))
    returns:
    (7 6 3 7)

    ...but it's also easy with DO:

    (defun do-pos+ (orig-list)
    (do ((i 0 (1+ i))
    (list orig-list (cdr list))
    (new-list nil (cons (+ (car list) i) new-list)))
    ((endp list) (nreverse new-list))))


    (define (do-pos+ orig-list)
    (do_ ((i 0 (+ 1 i))
    (x :in orig-list)
    (result :collect (+ i x)))
    (#f @ result)))

    (do-pos+ '(200 300 400))
    ===>
    (200 301 402)

    Given:

    (define-syntax do_-aux
    (syntax-rules ( <> @ :in :collect )
    [ (do_-aux ((x what <>) more ...) (seen ...) stuff ...)
    (do_-aux (more ...) (seen ... (x what what)) stuff ...) ]
    [ (do_-aux ((x :in seq) more ...) seen (lets ...) (bool z ...) stuff ...)
    (do_-aux ((x (and (pair? the-list) (car the-list)) <>) more ...)
    seen
    (lets ... (the-list seq))
    ((or (null? the-list) (begin (pop! the-list) #f) bool) z ...)
    stuff ...) ]
    [ (do_-aux ((accum :collect x) more ...) stuff ...)
    (do_-aux ((accum '() (cons x accum)) more ...) stuff ...) ]
    [ (do_-aux (spec more ...) (seen ...) stuff ...)
    (do_-aux (more ...) (seen ... spec) stuff ...) ]
    [ (do_-aux () seen lets (bool y ... @ result) stuff ...)
    (do_-aux () seen lets (bool y ... (reverse result)) stuff ...) ]
    [ (do_-aux () seen (lets ...) more ...)
    (let (lets ...)
    (do seen more ...))
    ] ))
    (define-syntax do_
    (syntax-rules ()
    [ (do_ specs more ...)
    (do_-aux specs () () more ...) ] ))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Fri Aug 8 00:08:20 2025
    B. Pym wrote:

    (defun mapcar-pos+ (list)
    (let ((i -1))
    (mapcar #'(lambda (elt) (+ elt (incf i)))

    iteration:

    (defun pos+ (lst)
    (setf acc NIL)
    (setf i 0)
    (dolist (obj lst)
    ; i know, instead of append, i could do a cons and reverse afterwards...
    (progn (setf acc (append acc (list (+ obj i))))
    (setf i (+ i 1))))
    acc)

    I'd prefer LOOP here:

    (defun loop-pos+ (list)
    (loop for i from 0
    for elt in list
    collect (+ elt i)))

    (defun pos+ (xs)
    (do ((i 0 (+ 1 i))
    r)
    ((not xs) (reverse r))
    (push (+ i (pop xs)) r)))

    --
    [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)
  • From B. Pym@21:1/5 to B. Pym on Fri Aug 22 10:56:33 2025
    B. Pym wrote:

    B. Pym wrote:

    (defun mapcar-pos+ (list)
    (let ((i -1))
    (mapcar #'(lambda (elt) (+ elt (incf i)))

    iteration:

    (defun pos+ (lst)
    (setf acc NIL)
    (setf i 0)
    (dolist (obj lst)
    ; i know, instead of append, i could do a cons and reverse afterwards...
    (progn (setf acc (append acc (list (+ obj i))))
    (setf i (+ i 1))))
    acc)

    I'd prefer LOOP here:

    (defun loop-pos+ (list)
    (loop for i from 0
    for elt in list
    collect (+ elt i)))

    (defun pos+ (xs)
    (do ((i 0 (+ 1 i))
    r)
    ((not xs) (reverse r))
    (push (+ i (pop xs)) r)))

    (define (pos+ xs)
    (Do ((i 0 (+ 1 i))
    r)
    ((null? xs) @ r)
    (push! r (+ i (pop! xs)))))

    Given:

    (define-syntax Do-aux
    (syntax-rules (<> @ values)
    [(_ ((a b <>) d ...) (seen ...) z ...)
    (Do-aux (d ...) (seen ... (a b b)) z ...) ]
    [(_ ((a b c ...) d ...) (seen ...) z ...)
    (Do-aux (d ...) (seen ... (a b c ...)) z ...) ]
    [(_ ((a) d ...) (seen ...) z ...)
    (Do-aux (d ...) (seen ... (a '())) z ...) ]
    [(_ (a d ...) (seen ...) z ...)
    (Do-aux (d ...) (seen ... (a '())) z ...) ]
    [(_ () seen (a b ... @ (values x ...)) z ...)
    (Do-aux () seen (a b ... (values (reverse~ x) ...)) z ...) ]
    [(_ () seen (a b ... @ xs) z ...)
    (Do-aux () seen (a b ... (reverse xs)) z ...) ]
    [(_ () seen till body ...)
    (do seen till body ...) ]))
    (define-syntax Do
    (syntax-rules ()
    [(_ specs till body ...)
    (Do-aux specs () till body ...) ]))

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