2013-09-26 42 views

回答

1

球拍,這是很容易使用內置的程序:

(define (pad-front n x lst) 
    (append     ; append together both lists 
    (build-list n (const x)) ; create a list with n repetitions of x 
    lst))     ; the list at the tail 

(pad-front 3 'a '(b c)) 
=> '(a a a b c) 

...但我猜你想從頭開始實現它。這個想法與上述相同,但只使用原始程序;我會給你一些提示,所以你可以弄清楚細節。填充這一空白:

(define (pad-front n x lst) 
    (if <???>   ; base case: if `n` is zero 
     <???>   ; then return the tail list 
     (cons <???>  ; else cons the element to be repeated 
      (pad-front ; and advance the recursion 
      <???>  ; subtract one unit from `n` 
      x lst)))) ; pass along `x` and `lst` 

注意,關鍵是要不斷增加的x元素,直到不需要更多的重複(換句話說:直到n爲零)。在那一點上,我們只是把尾部列表放在最後,我們就完成了!

+0

非常感謝你以前我在想什麼但謝謝你。 –

相關問題