AlexKnauth's comment is the one you need to be following – you must define and adhere to a strict domain (input) and codomain (output) for your function
;; from your code
(list Y
(convert-to-base-four X))
It doesn't matter what Y
and X
are here: if convert-to-base-four
returns a list of some value and a recursive call to convert-to-base-four
– which returns a list – you're going to end up with a list of lists!
One solution, as another points out, is to use append
– but beware, it's a trap
(define (base4 n)
(if (< n 4)
(list n)
(append (base4 (floor (/ n 4)))
(list (remainder n 4)))))
(displayln (base4 12)) ; (3 0)
(displayln (base4 13)) ; (3 1)
(displayln (base4 14)) ; (3 2)
(displayln (base4 15)) ; (3 3)
(displayln (base4 16)) ; (1 0 0)
(displayln (base4 123456)) ; (1 3 2 0 2 1 0 0 0)
A better solution would avoid the costly use of append
– here we do that using a named let loop
with two loop state variables m
and acc
(define (base4 n)
(let loop ((m n) (acc empty))
(if (< m 4)
(cons m acc)
(loop (floor (/ m 4))
(cons (remainder m 4) acc)))))
(displayln (base4 12)) ; (3 0)
(displayln (base4 13)) ; (3 1)
(displayln (base4 14)) ; (3 2)
(displayln (base4 15)) ; (3 3)
(displayln (base4 16)) ; (1 0 0)
(displayln (base4 123456)) ; (1 3 2 0 2 1 0 0 0)