William James
4/7/2015 5:15:00 AM
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)
In Scheme, reduce is usually called fold, so the opposite is
called unfold. It's found in Olin Shivers' SRFI-1.
The code below uses "cut", which enables abbreviated lambdas.
(The argument is indicated by <>, which is pronounced "slot".)
unfold accepts these arguments:
end-test key gen-next-seed seed
Gauche Scheme:
(use srfi-1) ; unfold
(unfold zero? (cut modulo <> 10) (cut div <> 10) 1984)
===>
(4 8 9 1)
Racket:
(require srfi/1) ; unfold
(require srfi/26) ; cut
(unfold zero? (cut modulo <> 10) (cut quotient <> 10) 1984)
===>
'(4 8 9 1)