2016-04-03 10 views
-2

私はLispを初めて使いました。特別な数を含むサブリストの2番目の値を返すことができる以上、関数を作成する必要があります。Lispで特別な番号を持つリストを取得する

たとえば、私は2つの引数を持つ関数を持っています。そのうちの一つは、サブリストと第二とのリストでは、私が検索するつもりだ私の特別な番号です:

(find_neighbours '((1 2) (3 1) (4 5) (9 1) (2 3) (1 5)) 1) 

機能はそのような何かを返す必要があること:

(2 3 9 5) 

我々は(1 2) (3 1) ...内1でサブリストを持っているので。ここで

は私のソリューションです:ここでは

(defun find_neighbours (lst node) 
    (if lst 
     (cond 
      ((= node (caar lst)) 
       (cons (cadar lst) 
       (find_neighbours (cdr lst) node)) 
      ) 
      ((= node (cadar lst)) 
       (cons (caar lst) 
       (find_neighbours (cdr lst) node)) 
      ) 
      (T (find_neighbours (cdr lst) node)) 
     ) 
    ) 
) 
+1

@sdsはすでに回答していますが、あなたがすでに持っているコード、試したこと、検索した場所などを表示することが期待されます。 – Rptx

答えて

0
があります。

私はこのように私の問題を解決した:

2

は単純なアプローチである:

(defun other-end (edge vertex) 
    "Return the other end of the EDGE if VERTEX is one of them or NIL." 
    (destructuring-bind (beg end) edge 
    (cond ((= vertex beg) end) 
      ((= vertex end) beg) 
      (t nil)))) 

(defun neighbors (graph vertex) 
    "Return the list of neighbors of the VERTEX in the GRAPH." 
    (loop for edge in edges 
    for other = (other-end edge vertex) 
    when other collect other)) 

他の方法、例えば、

(defun neighbors (graph vertex) 
    "Return the list of neighbors of the VERTEX in the GRAPH." 
    (delete nil (mapcar (lambda (edge) (other-end edge vertex)) 
         graph))) 

等...

関連する問題