拿出完美的广场

时间:2012-07-23 17:02:24

标签: scheme

如何使用过滤器和整数编写一个代码,从列表中删除完美的正方形?我相信它是建立在计划中的。例如。 (take-out-perfect-squares'(1 2 9 16 5 64))应返回(2 5)因为该列表中其他所有内容的平方根返回一个整数。谢谢!

2 个答案:

答案 0 :(得分:2)

(define remove-perfect-square-helper
  (lambda (x y)
    (if (null? x)
        (reverse y)
    (if (integer? (sqrt (car x)))
        (remove-perfect-square-helper (cdr x) y)
        (remove-perfect-square-helper (cdr x) (cons (car x) y))))))

(define remove-perfect-square
        (lambda (x)
          (remove-perfect-square-helper x '())))


(define x '(1 2 9 16 5 64))

(remove-perfect-square x)

答案 1 :(得分:0)

(define (take-out-perfect-squares l)
  (cond ((null? l) '())
        ((integer? (sqrt (car l)))
         (take-out-perfect-squares (cdr l)))
        (else
         (cons (car l) (take-out-perfect-squares (cdr l))))))