Scheme 格式中奇数重复的求解

Scheme 格式中奇数重复的求解,scheme,lisp,Scheme,Lisp,我试图解决在scheme中只输出列表中的非重复元素的问题。e、 g:”(a、b、a、c)将给出(a、b) 我已实施此解决方案: (define (remove L) (if (null? L) L (let ((firstL (car L)) (restL (cdr L))) (if (null? restL) (cons firstL ()) (if (equal? firstL (

我试图解决在scheme中只输出列表中的非重复元素的问题。e、 g:
”(a、b、a、c)
将给出
(a、b)

我已实施此解决方案:

(define (remove L)  
  (if (null? L)
      L
      (let ((firstL (car L)) (restL (cdr L)))        
        (if (null? restL)
            (cons firstL ())
            (if (equal? firstL (car restL))
                (remove (cdr restL))                
                (cons firstL (remove restL)))))))
但是,它也会输出重复次数为奇数的元素。e、 g:(a b a c)会给出(a b a)

我正试图修复它,但我做不到。我尝试过的解决方案之一的一个例子是:

(define (remove L)  
  (if (null? L)
      L      
      (let ((result ()) (firstL (car L)) (restL (cdr L)))        
        (if (null? restL)
            (result)            
            (if (equal? firstL (car restL))
                (remove2 firstL restL)                
                (append ((list firstL) result))
                (remove cdr restL)))))

  (define (remove2 x y)
    (cond ((null? y) (remove x)
                     ((equal? x (car y)) (remove2 ((car y) (cdr y))))                     
                     (else (remove x)))))

如果有人能想出一个解决方案,请写下来。

您的第二次尝试有很多语法错误。我认为,如果您查看第一次尝试,并用缺少的引号
'()
修复一个语法错误,如果您可以删除连续的元素而不是一个元素,那么您几乎就成功了。制造

(先修剪(a b c));=>(b)c
(先修剪’(a b c));=>(b)c
(先修剪’(a b c));=>(b)c
然后你可以用它代替cdr:

(定义(删除L)
(如果(空?L)
L
(让((第一辆车(第一辆车))(第二辆车(第二辆车)))
(如果(null?restL)
(cons firstL’())
(如果(等于第一辆(汽车剩余))
(拆卸(先装饰后恢复))
(反对第一条(删除第()()())))
我会使用
cond
来编写它,使它更平顺,并且远离非传统的
camelCase
,而支持
lisp case

(定义(删除管路lst)
(cond((null?lst)“”())
((空?(cdr lst))lst)
((相等?(车辆lst)(cadr lst))(拆下管路(先配平(cdr lst)))
(其他(cons(轿厢lst)(拆除管路(cdr lst()()))))

以下是使用“命名let”递归的Racket(方案派生)中的解决方案。已发送列表被划分为以下组件:
”((a)(b)(a)(c))
,然后只获取只有一项的子列表:

(define (f l)
  (let loop ((l (rest l))              ; first item removed from sent list
             (ol '())        ; store repeating sequences here as separate lists
             (temp (list(first l))))   ; keep first item in temp
    (cond
      [(empty? l)                      ; end reached
       ; (println (cons temp ol))      ; here temp+ol is '((c c)(a a a)(b)(a))
       (reverse
        (flatten
         (filter
          (λ (x) (= 1 (length x)) )    ; take only those which have only one item
          (cons temp ol))))]
      [(equal? (first l) (first temp)) ; if item repeats, store together
       (loop (rest l)
             ol
             (cons (first l) temp) )]
      [else                            ; else put temp in ol & start new temp
       (loop (rest l)
             (cons temp ol)
             (list (first l)))])))
测试:

(f '(a b a a a c c))  
输出:

'(a b)

trim first是方案中的内置功能吗?还是我必须实施它?@Maha不,你需要实施它。在我的回答中,我详细说明了它应该如何工作。我猜这是家庭作业,所以我担心如果我帮了你太多,我会毁了你的学业:)这不是我在准备期末考试。感谢您的帮助。(define(trim L2)(cond((null?L2))((equal?(car L2)(cadr L2))(trim(cdr L2)))(else(remove(cadr L2‘‘‘)’))我编写了这个函数,但它给出了一个错误,表示试图在c上应用cdr。这是我正在应用它的列表(a b a a c f)。