CS 61A HW07

文章展示了几个Lisp函数的定义和测试用例。filter-lst函数用于基于给定条件过滤列表;even?是一个判断数字是否为偶数的辅助函数;interleave函数将两个列表交错合并;accumulatecombiner实现累加操作;最后,no-repeats函数用于从列表中去除重复元素。
摘要由CSDN通过智能技术生成
(define (filter-lst fn lst)
  (cond ((null? lst) '())
        ((fn (car lst)) (cons (car lst) (filter-lst fn (cdr lst))) )
        (else (filter-lst fn (cdr lst))

        )
  )
)

;;; Tests
(define (even? x)
  (= (modulo x 2) 0))
(filter-lst even? '(0 1 1 2 3 5 8))
; expect (0 2 8)


(define (interleave first second)
  (cond ((null? first) second ) 
        ; ((null? second) first)
        (else (cons (car first) (interleave second (cdr first))))
  )
)

(interleave (list 1 3 5) (list 2 4 6))
; expect (1 2 3 4 5 6)

(interleave (list 1 3 5) nil)
; expect (1 3 5)

(interleave (list 1 3 5) (list 2 4))
; expect (1 2 3 4 5)


(define (accumulate combiner start n term)

  (cond ((= n 0) start )
        (else (accumulate combiner (combiner (term n) start) (- n 1) term))
        )
)


(define (no-repeats lst)
  (if (null? lst) lst
      (cons (car lst) 
        (no-repeats (filter (lambda (x) (not (= (car lst) x))) (cdr lst))))
  )
)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值