1

次のコードがあります。

 (define (atom? x)
   (and (not (null? x))
   (not (pair? x)))) 


 (define delete 
   (lambda (atom l)
     (cond
       ((null? l) '())
       ((atom? l) 
        (cond 
          ((not(eq? atom l)) l)
          (else '())
        )
       ) 
       (else (cons (delete atom (car l)) (delete atom (cdr l))) )
     )
   )  
  ) 

目標は、このリストから特定の文字を削除することです。例えば、

 (delete 'a '(a b a) )  ==> (b)

これの代わりに私は得ています:

  (delete 'a '(a b a) )==> (() b ())

私はスキームが初めてです。値が見つかった場合は何も返さないようにしましたが、次のように動作します。

 (delete 'a '(a b a) )==> (#<void> b #<void>)

何か案は?どうもありがとう!

4

1 に答える 1

0
 (define delete
      (lambda (atom l)
           (cond
                ((null? l) '())
                ((atom? (car l))
                     (cond
                          ((eq? atom (car l)) (delete atom (cdr l)))
                          (else (cons (car l) (delete atom (cdr l))))

                     )
                )
                (else (cons (delete atom (car l)) (delete atom (cdr l))))
            )
      )
  )  

このソリューションは、より基本的であり、基礎から理解しやすいものです。

于 2014-12-18T02:12:53.040 に答える