计划作业

发布于 2024-08-21 20:39:27 字数 73 浏览 4 评论 0原文

如何设计一个功能内容 输入一个原子列表 lat 并返回 lat 的内容。因此 '(abcabcdd) 的内容是 '(abcd)。

how to design a function content which
inputs a single list of atoms lat and which returns
the content of lat.Thus the content of '(a b c a b c d d) is '(a b c d).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

过去的过去 2024-08-28 20:39:28

下面的过程content应该可以满足您的需要。

(define (work x y)
  (if (null? (cdr x))
      (if (in? (car x) y)
          y 
          (cons (car x) y))
      (if (in? (car x) y)
          (work (cdr x) y)
          (work (cdr x) (cons (car x) y)))))

(define (in? x y)
  (if (null? y)
      #f
      (if (equal? x (car y))
          #t
          (in? x (cdr y)))))

(define (content x) (work x (list)))

过程content接受一个列表作为参数。它将列表发送到另一个名为 work 的过程。此过程处理列表并将列表中的项目添加到新列表中(如果它们尚不在新列表中)。 work 过程使用了另一个名为 in 的过程,该过程检查某个项目是否是列表的成员。

我的解决方案本质上将您的问题分为两个子问题,并使用比原始问题抽象级别更低的过程。

希望有帮助。

The procedure content below should get you what you need.

(define (work x y)
  (if (null? (cdr x))
      (if (in? (car x) y)
          y 
          (cons (car x) y))
      (if (in? (car x) y)
          (work (cdr x) y)
          (work (cdr x) (cons (car x) y)))))

(define (in? x y)
  (if (null? y)
      #f
      (if (equal? x (car y))
          #t
          (in? x (cdr y)))))

(define (content x) (work x (list)))

The procedure content accepts a list as a parameter. It sends the list to another procedure called work. This procedure processes the list and adds the items in the list to a new list (if they are not already in the new list). The work procedure makes use of yet another procedure called in, which checks to see if an item is a member of a list.

My solution essentially divides your problem into two sub-problems and makes use of procedures which operate at a lower level of abstraction than your original problem.

Hope that helps.

柠檬心 2024-08-28 20:39:28

这是PLT方案解决方案:

(define (is_exists list element)
 (cond
   [(empty? list) false]
   [else
     (cond 
       [(= (first list) element) true]
       [else (is_exists (rest list) element)])]))

(define (unique list target)
  (cond 
    [(empty? list) target]
    [else
     (cond 
       [(is_exists target (first list)) (unique (rest list) target)]
       [else (unique (rest list) (cons (first list) target))])]))

(define (create_unique list)
        (unique list empty))

检查一下:

> (define my_list (cons '1 (cons '2 (cons '3 (cons '2 (cons '1 empty))))))
> my_list
(list 1 2 3 2 1)
> (create_unique my_list)
(list 3 2 1)

It is PLT Scheme solution:

(define (is_exists list element)
 (cond
   [(empty? list) false]
   [else
     (cond 
       [(= (first list) element) true]
       [else (is_exists (rest list) element)])]))

(define (unique list target)
  (cond 
    [(empty? list) target]
    [else
     (cond 
       [(is_exists target (first list)) (unique (rest list) target)]
       [else (unique (rest list) (cons (first list) target))])]))

(define (create_unique list)
        (unique list empty))

Check it:

> (define my_list (cons '1 (cons '2 (cons '3 (cons '2 (cons '1 empty))))))
> my_list
(list 1 2 3 2 1)
> (create_unique my_list)
(list 3 2 1)
鱼忆七猫命九 2024-08-28 20:39:28

小阴谋家风格怎么样,

(define (rember-all a lat)
  (cond
    ((null? lat) '())
    ((eq? a (car lat)) (rember-all a (cdr lat)))
    (else (cons (car lat) (rember-all a (cdr lat))))))

(define (content lat)
  (cond
    ((null? lat) '())
    (else (cons (car lat)
                (content (rember-all (car lat) (cdr lat)))))))

How about little schemer style,

(define (rember-all a lat)
  (cond
    ((null? lat) '())
    ((eq? a (car lat)) (rember-all a (cdr lat)))
    (else (cons (car lat) (rember-all a (cdr lat))))))

(define (content lat)
  (cond
    ((null? lat) '())
    (else (cons (car lat)
                (content (rember-all (car lat) (cdr lat)))))))
沒落の蓅哖 2024-08-28 20:39:28

从简单地创建传入列表的副本的过程开始(非常容易做到):

(define (unique-elements seq)
  (define (loop ans rest)
    (cond ((null? rest) ans)
          (else
           (loop (cons (car rest) ans)
                 (cdr rest)))))
  (loop '() seq))

为了确保输出列表的元素是唯一的,如果 REST 的头已经是 ANS 的成员,我们应该跳过 CONS。因此我们添加另一个条件来做到这一点:

;;; Create list containing elements of SEQ, discarding duplicates.
(define (unique-elements seq)
  (define (loop ans rest)
    (cond ((null? rest) ans)
          ((member (car rest) ans)  ; *new*
           (loop ans (cdr rest)))   ; *new*
          (else
           (loop (cons (car rest) ans)
                 (cdr rest)))))
  (loop '() seq))

Start from a procedure that simply creates a copy of the passed-in list (very easy to do):

(define (unique-elements seq)
  (define (loop ans rest)
    (cond ((null? rest) ans)
          (else
           (loop (cons (car rest) ans)
                 (cdr rest)))))
  (loop '() seq))

To ensure that the output list's elements are unique, we should skip the CONS if the head of REST is already a member of ANS. So we add another condition to do just that:

;;; Create list containing elements of SEQ, discarding duplicates.
(define (unique-elements seq)
  (define (loop ans rest)
    (cond ((null? rest) ans)
          ((member (car rest) ans)  ; *new*
           (loop ans (cdr rest)))   ; *new*
          (else
           (loop (cons (car rest) ans)
                 (cdr rest)))))
  (loop '() seq))
一杆小烟枪 2024-08-28 20:39:28

以下函数接受一个列表并使用递归返回一个新列表,其中仅包含其参数的唯一输入:

(defun uniq (list)
    (labels ((next (lst new)
                (if (null lst)
                    new
                    (if (member (car lst) new)
                        (next   (cdr lst) new)
                        (next   (cdr lst) (cons (car lst) new))))))
        (next list ())))

正如评论中提到的,common lisp 已经具有此函数:

(defun uniq (list)
    (remove-duplicates list))

The following function takes in a list and returns a new list with only the unique inputs of it's argument using recursion:

(defun uniq (list)
    (labels ((next (lst new)
                (if (null lst)
                    new
                    (if (member (car lst) new)
                        (next   (cdr lst) new)
                        (next   (cdr lst) (cons (car lst) new))))))
        (next list ())))

As was mentioned in the comments, common lisp already has this function:

(defun uniq (list)
    (remove-duplicates list))
一个人练习一个人 2024-08-28 20:39:28
(define (remove-duplicates aloc)
  (cond
    ((empty? aloc) '())
    (else (cons (first aloc)
                (remove-duplicates
                 (filter (lambda (x)
                           (cond
                             ((eq? x (first aloc)) #f)
                             (else #t)))
                         (rest aloc)))))))
(define (remove-duplicates aloc)
  (cond
    ((empty? aloc) '())
    (else (cons (first aloc)
                (remove-duplicates
                 (filter (lambda (x)
                           (cond
                             ((eq? x (first aloc)) #f)
                             (else #t)))
                         (rest aloc)))))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文