• Re: doubling list elements at every level using recursion

    From B. Pym@21:1/5 to Pascal J. Bourguignon on Wed Jul 17 15:32:54 2024
    Pascal J. Bourguignon wrote:

    You could slightly generalize double, and accept any atoms. This would
    allow you to simplify it.

    You could also use emacs and let it do the indentation for you!

    (defun double (object)
    (typecase object
    (cons (cons (double (car object)) (double (cdr object))))
    (number (* 2 object))
    (t object)))

    (mapcar (function double)
    '( () abc 123 (1 2.0 #C(3 4) a b c (5/2 6 d e f) 7 8 9) ))
    (NIL ABC 246 (2 4.0 #C(6 8) A B C (5 12 D E F) 14 16 18))


    Why not simply

    (double '(() abc 123 (1 2.0 #C(3 4) a b c (5/2 6 d e f) 7 8 9)))
    ===>
    (NIL ABC 246 (2 4.0 #C(6 8) A B C (5 12 D E F) 14 16 18))


    Scheme

    (define (double obj)
    (cond ((pair? obj) `(,(double (car obj)) ,@(double (cdr obj))))
    ((number? obj) (* 2 obj))
    (#t obj)))

    (double 3)
    ===>
    6

    (double '(() abc 123 (1 2.0 (3 4) a b c (5/2 6 d e f) 7 8 9)))
    ===>
    (() abc 246 (2 4.0 (6 8) a b c (5 12 d e f) 14 16 18))

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