函数应该获取整数列表并返回包含两个子列表的列表 - 第一个包含原始列表中的偶数,第二个包含奇数。我的代码完成了工作,但如果我用负整数测试它,例如第二次测试中的-5,那么我的代码就会忽略它。有关如何修复的任何想法?
(旁注 - 我知道有偶数,奇数等功能,但对于这项任务,我自己创建它们。)
(define (segregate lst)
(list(pullEven lst)(pullOdd lst)))
(define (pullEven lst)
(if (empty? lst)
'()
(if (isEven (first lst))
(cons (first lst) (pullEven (rest lst)))
(pullEven (rest lst)))))
(define (pullOdd lst)
(if (empty? lst)
'()
(if (isOdd (first lst))
(cons (first lst) (pullOdd (rest lst)))
(pullOdd (rest lst)))))
(define (isEven x)
(if (equal? (remainder x 2) 0) #t #f)
)
(define (isOdd x)
(if (equal? (remainder x 2) 1) #t #f)
)
;tests
"---------------------------------------------"
"Segregate Tests"
(segregate '(7 2 3 5 8))
(segregate '(3 -5 8 16 99))
(segregate '())
"---------------------------------------------"