2012-01-30 127 views
1

我試圖從n位置獲取元素到m位置。但我得到一些錯誤。 我該如何解決?Lisp從列表中獲取元素

(defun element (list start end) 
(dotimes (start end) (n-esimo list start) 
)) 

(defun n-esimo (l n) 
    (cond ((equal n 0) (first l)) 
     (t (n-esimo (rest l) (- n 1))))) 
+0

你會得到什麼錯誤? – 2012-01-30 23:32:38

+0

獲取NIL。沒有錯誤,但只有零和我的列表不是空的 – marchetto91 2012-01-30 23:41:03

+0

'N-esimo'已經存在於Common Lisp標準中,它被稱爲'nnth'(儘管它反過來需要參數)。 「Dotimes」不符合你的想法。在Hyperspec中查看它,例如通過google搜索「clhs dotimes」。 – Svante 2012-01-31 20:55:35

回答

5

有沒有必要寫它(除非你的任務是你的功課,你應該然後通過添加標籤homework你的問題,或其他教育目的告訴我們的一部分)。 Common Lisp內置了這個功能。它被稱爲SUBSEQ