2011-08-17 63 views
2

我不得不使用LISP或PROLOG從列表中刪除元素的外觀。使用Lisp或PROLOG從列表中刪除元素的外觀

下面是一些例子。

輸入: '(5 2(3 5(3)5(4×2(2 4)))5 2)

輸出:'(5 2(3(5分)(4(2)) ))

輸出列表的結構保持不變。

謝謝你的建議,

+4

你可能忘了'homework'標籤。 –

+0

它不是從數據中不知道你想要刪除什麼元素,你從2s,3s,4s和5s開始,你完成了與現在相同的元素。然而,這個問題的描述表明,一個人將完全消失。 – Vatine

+0

我希望列表中每個元素的每一個外觀都被刪除。這意味着第二,第四等...列表的結構必須保持不變。 (** 5 **(** 3 **)5(4 ** 2 **(2 ** 4 **)))** ** ** ** ** 2 **) 必須刪除大膽的項目。 – Unknown

回答

2

因爲這似乎是一門功課,我將只提供一個指向解決方案:

  • 審查REMOVE-IF謂語Common Lisp中。 (它可能不會做你需要的一切......)
  • 建立一個遞歸步行者。
  • 考慮你將如何來回傳遞狀態。

另外,我強烈建議您張貼工作片段並詢問具體問題。像上面這樣一個普遍的問題似乎表明你想要一個解決方案落在你的大腿上。

好吧,這不是作業。我在智力上受到了好奇。我解決了它。

這是一個遞歸步行者。如果你的Lisp有TCO,它應該可以轉化爲TCO。

你可以在循環中做到這一點,但這需要維護一個棧列表來處理「進入列表」的情況。

我對一個慣用的解決方案沒有要求。


(defparameter input '(5 2 (3 5 (3) 5 (4 2 (2 4))) 5 2)) 

(defparameter output '(5 2 (3() 5 (4 (2))))) 

(defun remove-every-other-appearence (list &optional (hash-table nil)) 
    (when list        ; termination condition 
    ;(format t "~%~a~&" list) 
    (unless hash-table      ; if it's the first time 
     ;(format t "creating hash table~&") 
     (setf hash-table (make-hash-table))) ; create a hash table 
    (let ((eut (car list)))    ; element under test 
     ;(format t "eut: ~a~&" eut) 
     (cond 
     ((consp eut)      ;Recursion time, it's a list. 
     ;(format t "Recursing~&") 
     (cons 
      (remove-every-other-appearence eut hash-table) 
      (remove-every-other-appearence (cdr list) hash-table))) 
     (t        ;Otherwise... 
             ; Increment seen counter by 1 
     (setf (gethash eut hash-table 0) 
       (1+ (gethash eut hash-table 0))) 
     (cond 
      ((evenp (gethash eut hash-table)) 
      (remove-every-other-appearence (cdr list) hash-table)) 
             ;ignore the element 
      ((oddp (gethash eut hash-table)) 
             ; if this is the odd occurance 
             ;cons the element back onto the rest of the list 
      (cons eut (remove-every-other-appearence (cdr list) hash-table)))   
      (t 
      (error "This integer is neither even nor odd. We're in trouble")))))))) 

* input 

(5 2 (3 5 (3) 5 (4 2 (2 4))) 5 2) 
* (remove-every-other-appearence input) 

(5 2 (3 NIL 5 (4 (2)))) 
* output 

(5 2 (3 NIL 5 (4 (2)))) 
+0

好的,這不是家庭作業,如果是我不希望我的作業被其他人寫作。我只想幫助理解如何做到這一點,因爲我沒有任何想法。例如給我僞碼。 感謝您的建議, – Unknown

+0

@未知:你必須走清單。我在工作中的閒暇時間(閒扯,哈哈)閒聊着。 –

+0

@未知:享受解決方案。 Prolog應該是一個簡單的轉換。 –

0

FWIW,您的問題聲明比較混亂。

正如我所理解的問題,問題是你有一個任意的「樹」,其中的非葉節點是列表和葉節點是別的東西。一個「列表清單」。你想要看作爲一個邏輯扁平序列的葉節點,迭代它並刪除每個其他葉節點而不是改變了「樹」的整體形狀,使得唯一改變的是數量葉子掛在任何給定的節點上。

從這一點,給出下面的輸入,你會實現相應的輸出:

input: [] 
output: [] 

input: [a,b,c,d] 
output: [a,c] 

input: [a,[],b,c,d] 
output: [a,[],c] 

input: [a,[b],c,d] 
output: [a,[],c] 

input: [a,[b,c,d,e],f,g] 
output: [a,[c,e],g] 

input: [a,[b,[],[c,d,[e,f,g],h],i],j] 
output: [a,[[],[c,[e,g]],i]] 

下面是一個[未測試]序言溶液。其中,我只是維護兩個狀態,keepremove並根據需要在它們之間切換。你可以免費獲得奇數/偶數:這取決於你啓動機器的狀態。

應該注意的是,如果傳入的數據結構包含任何未綁定/非統一變量,那麼您就不會得到正確的結果。不需要的統一會導致問題。警衛條款將需要添加到正確處理與他們。

% ==================== 
% The public interface 
% ==================== 
remove_even(Xs , Ys) :- remove_every_other_node(keep , _ , Xs , [] , Ys). 
remove_odd( Xs , Ys) :- remove_every_other_node(remove , _ , Xs , [] , Ys). 

%------------------ 
% The core iterator 
%------------------ 
remove_every_other_node(S , S , [] , T , List) :- 
    reverse(T,List) 
    . 
remove_every_other_node(S , N , [X|Xs] , T , List) :- 
    process_node(S, S1 , X , T , T1) , 
    remove_every_other_node(S1 , N , Xs , T1 , List) 
    . 

%---------------------- 
% The list node handler 
%---------------------- 
process_node(S , S , []  , T , [[]|T]). 
process_node(S , N , [X|Xs] , T , [L1|T]) :- 
    remove_every_other_node(S , N , [X|Xs] , [] , L1) 
    . 
process_node(keep , remove , X , T , [X|T]). 
process_node(remove , keep , X , T , T ). 
+0

你說得對,也許我的問題還不夠清楚。我必須刪除列表中的第二,第四等重複。所以我得到第一個元素,並尋找列表中的第二個,第四個外觀,如果我有,我刪除了這些元素,之後,我得到第二個元素,並重復以前的過程。 – Unknown

0
(defun rea (tree) 
    (let ((table (make-hash-table))) 
    (labels ((fn (x) 
       (unless (and x (atom x) (evenp (incf (gethash x table 0)))) 
       (list (if (atom x) x (mapcan #'fn x)))))) 
     (car (fn tree)))))