English 中文(简体)
先从LISP的名单中删除原子,然后从清单中除名。
原标题:To sort out atoms first and then sublists from a list in LISP

我在LISP有这种家庭工作,我需要从名单上剔除原子,然后从名单上除名。 我确信,这应该是轻而易举的任务,但因为我不是一位方案家中的多数人,因此,这在我理解的时候确实是算的。

我有这份名单:

(5 -1 (2 6 1) (8 7 -3) (0 (9 4)) -6)

And if I understand correctly my task then I should get something like this:

(5 -1 -6 (2 6 1) (8 7 -3) (0 (9 4)))

迄今为止,我发现的都是如何算出原子和(或)子清单,但我不需要。

(DEFUN ATOMNUMBER (L) (COND ((NULL L) 0)
  ((ATOM (CAR L)) (+ 1 (ATOMNUMBER (CDR L))))
  (T (ATOMNUMBER (CDR L))) ))

而且,即使在只有分项、只有原子或空洞清单的情况下,这一职能也应正确发挥作用。

难道有人能给我任何例子吗?

提前感谢!

最佳回答

我更多地用于该计划,但这里的解决办法是在利什:

(defun f (lst)
  (labels 
      ((loop (lst atoms lists)
         (cond
          ((null lst) 
           (append (reverse atoms) (reverse lists)))
          ((atom (car lst))
           (loop (cdr lst) (cons (car lst) atoms) lists))
          (T
           (loop (cdr lst) atoms (cons (car lst) lists))))))
    (loop lst  ()  ())))

(f  (5 -1 (2 6 1) (8 7 -3) (0 (9 4)) -6))

基本上,你在名单上重复了,每个要素都列入原子名单或清单。 最后,你们都同意取得结果。

http://www.ohchr.org。

删除的版本当然缩短:

(let ((l  (5 -1 (2 6 1) (8 7 -3) (0 (9 4)) -6)))
   (append
    (remove-if-not # atom l)
    (remove-if     # atom l)))
问题回答

共同许可证办法有几种可能的做法:

  • REMOVE-IF去除不想要的物品。 (以替代方式使用REMOVE-IF-NOT来保存被通缉的物品。) 你们需要两个名单。 申请。

  • 利用DOLIST并在清单中储存,将项目收集到两个清单并附上。

  • 在你需要保留两份结果清单的情况下,撰写一份复习程序。

  • 还应当能够使用具有特殊地位的SORT。

Example:

> (sort  (1 (2 6 1) 4 (8 7 -3) 4 1 (0 (9 4)) -6 10 1)
        (lambda (a b)
           (atom a)))

(1 10 -6 1 4 4 1 (2 6 1) (8 7 -3) (0 (9 4)))

稳定版本:

(stable-sort  (1 (2 6 1) 4 (8 7 -3) 4 1 (0 (9 4)) -6 10 1)
             (lambda (a b)
               (and (atom a)
                    (not (atom b)))))

(1 4 4 1 -6 10 1 (2 6 1) (8 7 -3) (0 (9 4)))

Just in case you will want to exercise more, and you will find that the examples provided here are not enough :P

(defun sort-atoms-first-recursive (x &optional y)
  (cond
    ((null x) y)
    ((consp (car x))
     (sort-atoms-first-recursive (cdr x) (cons (car x) y)))
    (t (cons (car x) (sort-atoms-first-recursive (cdr x) y)))))

(defun sort-atoms-first-loop (x)
  (do ((a x (cdr a))
       (b) (c) (d) (e))
      (nil)
    (if (consp (car a))
      (if b (setf (cdr b) a b (cdr b)) (setf b a d a))
      (if c (setf (cdr c) a c (cdr c)) (setf c a e a)))
    (when (null (cdr a))
      (cond
        ((null d) (return e))
        ((null c) (return d))
        (t (setf (cdr b) nil (cdr c) d) (return e))))))


(sort-atoms-first-recursive  (5 -1 (2 6 1) (8 7 -3) (0 (9 4)) -6))

(sort-atoms-first-loop  (5 -1 (2 6 1) (8 7 -3) (0 (9 4)) -6))

第二项是破坏性的(但并未产生任何新的组合)。

此处是代号,以自上而下的方式构造其产出(评论见Haskell syntax):

;atomsFirst xs = separate xs id id where
;  separate [] f g  = f (g [])
;  separate (x:xs) f g
;      | atom x = separate xs (f.(x:)) g
;      | True   = separate xs f (g.(x:))

(defmacro app (l v)
   `(progn (rplacd ,l (list ,v)) (setq ,l (cdr ,l))))

(defun atoms-first (xs)
  (let* ((f (list nil)) (g (list nil)) (p f) (q g))
    (dolist (x xs)
      (if (atom x) (app p x) (app q x)))
    (rplacd p (cdr g))
    (cdr f)))

正在以自上而下的方式编制的两份中间名单,基本上沿用了不同名单模式,作为不限成员名单(即有明确的终点站)。

你们可以采取这种休养方式:

(defun f (lst) 
    (cond 
        ((null lst) nil)
        ((atom (car lst)) 
        (append (list (car lst)) (f (cdr lst)))) 
        (T
            (append (f (cdr lst)) (list (f (car lst))))
        )
    )
)
(step (f  (5 -1 (2 6 1) (8 7 -3) (0 (9 4)) -6)))

产出:

step 1 --> (F  (5 -1 (2 6 1) (8 7 -3) ...))                                                                   
step 1 ==> value: (5 -1 -6 (0 (9 4)) (8 7 -3) (2 6 1))




相关问题
Lisp code called from Java

Long story: I am doing a project for my functional programing class, and I thought of writing an AI controller in Lisp, for the Mario AI competition. I was looking over frameworks/libraries/ways of ...

Emacs, Zen-Coding mode, and Putty

I use emacs via Putty and since Putty doesn t send certain key combinations to the remote console I generally need to re-bind them to other key combinations. After installing the amazing Zen-Coding ...

In Which Cases Is Better To Use Clojure? [closed]

I develop in Lisp and in Scheme, but I was reading about Clojure and then I want to know, in which cases is better to use it than using Lisp or Scheme? Thanks

lambda-gtk negative pointer

I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative ...

Is there a common lisp package naming convention?

I have created some of my own user packages and have run into a name clash. In Java, the naming convention is to use your domain name in the package name: e.g. import com.example.somepackage;. Are ...

SOAP request from within an AutoLISP/AutoCAD macro

We have built a webservice for a client that uses AutoCAD. They have a macro that runs in AutoCAD that builds a SOAP request. But they have not figured out how to actually send() the soap request to ...

热门标签