方案/球拍中的功能和列表

时间:2012-01-06 07:07:39

标签: list function scheme racket

如何定义一个带有一个参数的函数,该参数应该是一个列表,并返回该元素 列表本身就是列表?

(check-expect (find-sublists ’(1 2 () (3) (a b c) a b c)) 
              ’(() (3) (a b c)))

2 个答案:

答案 0 :(得分:4)

您是否有设计能够通过列表过滤的功能的经验?

与原始版本具有相同风格的更简单问题是这样的:设计一个函数,该函数采用数字列表并仅保留偶数。你能做到这个功能吗?

查看http://www.ccs.neu.edu/home/matthias/HtDP2e/htdp2e-part2.html并参加其指导练习也可能会有所帮助。

答案 1 :(得分:2)

两个有用的工具,应该让你开始:

1)浏览列表:

; traverse: takes a list of numbers
; Goes through each element, one-by-one, and alters it
(define traverse
  (lambda (the_list)
    (if (empty? the_list)
        empty
        (cons (+ 1 (first the_list)) 
              (traverse (rest the_list))))))

(traverse (cons 3 (cons 4 empty)))返回(cons 4 (cons 5 empty))

2)list?

(list? (list 1 2 3))返回#t
(list? 5)返回#f

相关问题