2014-11-23 65 views
1

我必須創建一個名爲(list-push-front lst new-list)的過程,它將新列表中的元素添加到lst的前面。例如,輸出爲:(list-push-front'(4 3 7 1 2 9)'(1 2))應該給出
'(1 2 4 3 7 1 2 9)LISP追加方法

這就是我有這麼遠,但我收到的元數錯誤消息的參數(2)預期數量預期(1)

(define(list-push-front lst new-list) 
 
    (if(null? lst) 
 
    '() 
 
    (append(list-push-front(car new-list))(lst(car lst)))))

+0

出了什麼問題只是'(追加新的列表LST )'? – xbug 2014-11-24 02:20:52

+0

這裏只給出'list-push-front'一個參數:'(list-push-front(car new-list))'。你也嘗試在這裏應用'lst'作爲函數:'(lst(car lst))'。如果您使用的是DrRacket,它可以突出「級別」中的缺口以幫助您查看代碼的結構。 – molbdnilo 2014-11-24 12:19:23

回答

3

只要打電話給append過程中的給定數量的不匹配時,不正是你需要什麼 - 當使用新程序時,你應該總是參考documentation。在這種情況下,我們沒有寫一個明確的遞歸,使用內置的功能就足夠了:

(define (list-push-front lst new-list) 
    (append new-list lst)) 

例如:

(list-push-front '(4 3 7 1 2 9) '(1 2)) 
=> '(1 2 4 3 7 1 2 9)