• Re: List of digits->number

    From B. Pym@21:1/5 to Frank Buss on Sat Sep 21 13:59:34 2024
    XPost: comp.lang.scheme

    Frank Buss wrote:

    (defun split-backward (number &optional (base 10))
    (loop while (< 0 number) collect
    (multiple-value-bind (quotient remainder) (floor number base)
    (setf number quotient)
    remainder)))

    LOOP is ok, but I wonder if there is a more elegant contruct like REDUCE
    for the opposite concept for building a list. Building the list should not need more program code characters than reducing the list.

    Testing:

    * (split-backward 1984)

    (4 8 9 1)


    Gauche Scheme

    (use srfi-1) ; unfold

    (unfold zero? (cut mod <> 10) (cut quotient <> 10) 1984)
    ===>
    (4 8 9 1)

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

    B. Pym wrote:

    Frank Buss wrote:

    (defun split-backward (number &optional (base 10))
    (loop while (< 0 number) collect
    (multiple-value-bind (quotient remainder) (floor number base)
    (setf number quotient)
    remainder)))

    LOOP is ok, but I wonder if there is a more elegant contruct like REDUCE for the opposite concept for building a list. Building the list should not need more program code characters than reducing the list.

    Testing:

    * (split-backward 1984)

    (4 8 9 1)


    Gauche Scheme

    (use srfi-1) ; unfold

    (unfold zero? (cut mod <> 10) (cut quotient <> 10) 1984)
    ===>
    (4 8 9 1)

    (do ((n 1984 (div n 10))
    (r () (cons (mod n 10) r)))
    ((zero? n) r))

    ===>
    (1 9 8 4)

    --
    [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 (05 Dec 2004)

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