连接列表元素 - Scheme

Concatenating list elements - Scheme

如果我有一个生成以下结果的方案代码:(我正在使用 cons)

'((1 . 0) . 0)

我怎么能接受这个,只是简单地显示 100,就好像它只是一个整数,而不是一个带有这些点和括号的列表?

谢谢!

编辑:

我的完整代码:

(define (func X) 
    (if ( <= X 3 )
       X
            (cons (modulo X 4) (func(floor(/ X 4)) ))  
))

如果我没理解错的话,您正在尝试将一个以 10 为底的数字转换为以 4 为底的数字,然后将其显示为数字,但您的实现存在几个问题。

您正在构建一个列表作为输出 - 但这不是您想要的,您需要一个 数字。此外,您以错误的顺序遍历输入,这不是找到两个数字之间的商的正确方法。也许这会有所帮助:

(define (func X)
  (let loop ((n X) (acc 0) (mult 1))
    (if (< n 4)
        (+ (* mult n) acc)
        (loop (quotient n 4)
              (+ (* mult (modulo n 4)) acc)
              (* mult 10)))))

或者,您可以输出一个 string 来强调输出不以 10 为基数的事实:

(define (func X)
  (let loop ((n X) (acc ""))
    (if (< n 4)
        (string-append (number->string n) acc)
        (loop (quotient n 4)
              (string-append (number->string (modulo n 4)) acc)))))

它将按预期工作:

(func 16)
=> 100

Oscar Lopez 的回答非常好。我忍不住补充说这个问题不需要 "loop" 构造:

;; translate a string to a base-4 string.
(define (func n)
  (cond [(< n 4) (number->string n)]
        [else (string-append (func (quotient n 4))
                                   (number->string (modulo n 4)))]))