# 如何从一个给定列表提取出无穷多组组合

a + b + c =30 从一个列表中(1,3,5,7,11,13,15) 中提取三个元素，使其满足上式，该如何做？ 第二个问题，如果我想从这7个元素中提取出来2个元素任意组合,3个,4个 我又该如何做？

## scheme and lisp的一些区别点

1. 在Common Lisp眼中,一个符号的symbol-value和symbol-function是不一样的,而Scheme对两者不作区分。在Scheme里面，变量只有唯一对应的值，它可以是个函数，也可以是另一种对象。因此，在Scheme中就不需要#’或者funcall了。Common Lisp的：

`````` (let ((f #’(lambda (x) (1+ x))))
(funcall f 2))
``````

``````    (let ((f (lambda (x) (1+ x))))
(f 2))
``````

## 记住在Drracket Help中Pairs and Lists有更多的关于列表的操作

1. remove-duplicate：去除一个lat中重复的元素
2. reduce 把一个最小包含()的列表 比如((a b) (c d)) 会变为(a b c d) ((a (b)) (c d)) ==>(a (b) c d)
3. sort: 排序，暂时没时间

## P07 Flatten a nested list structure.

Transform a list, possibly holding lists as elements into a flat list by replacing each list with its elements (recursively).

Try Again you.

## P08 Flatten a nested list structure.

Transform a list, possibly holding lists as elements into a flat list by replacing each list with its elements (recursively)

(define (atom? x)

``````(and (not (null?  x)) (not (pair?  x))))
``````

## P09 Eliminate consecutive duplicates of list elements.

If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.

## P12 Modified run-length encoding.

;;;;Modify the result of problem P10 in such a way that if an element has no duplicates it is ;;;simply copied into the result list. Only elements with duplicates are transferred as (N E) lists.

;;;;然而这时候我们 又需要尝试反着去处理那些数据

## P13 Decode a run-length encoded list.

;;;Given a run-length code list generated as specified in problem P11. Construct its uncompressed version

## P14 Run-length encoding of a list (direct solution).

Implement the so-called run-length encoding data compression method directly. I.e. don’t explicitly create the sublists containing the duplicates, as in problem P09, but only count them. As in problem P11, simplify the result list by replacing the singleton lists (1 X) by X.

## P18 Split a list into two parts ;the length of the first part is given

;;;1 共有模式选取一个表 从0选到n即可 ;;2 提供一个表 利用nthcdr 该过程接受两个参数 第一个参数表示开始位置 第二个参数表示总表 ,截取到表的截止位置 ;;> (nthcdr 3 ‘(a b c d e)) —–>测试成功 ;;;’(d e)

## P19 Extract a slice from a list given tow indice,I and K ,the slice is the list containing the elements

``````;; between the I'th and K'th element of the original list (both limits incluede);;类似于subseq
``````

## P20 rotate a list N places to the left

cut the table into two parts,then change the position

## P27 Generate the combinations of K distinct objects chosen from the N elements of a list?

in how many ways can a committee of 3 be chosen from a group of 12 people? 数学力的排列组合C(12,3)=220种可能。

1. 选择第0个元素,在剩下的链表中选取n-1个元素
2. 选择第1个元素,在剩下的链表中选取n-1个元素。。依次递归
3. n=1 选取当前链表的第一个元素

## P28 Group the elements of a set into disjoint subsets

• In how many ways can a group of 9 people work in 3 disjoint subgroups of 2,3 and 4 persons? write a function that generates all the possibilities and returns them in a list.

• Example (group3 ‘(aldo beat carla david evi flip gary hugo ida)) ( ( (ALDO BEAT) (CARLA DAVID EVI) (FLIP GARY HUGO IDA) )… )

• Generalize the above predicate in a way that we can specify a list of group sizes and the predicate will return a list of groups.

Example: (group ‘(aldo beat carla david evi flip gary hugo ida) ’(2 2 5))
(((ALDO BEAT) (CARLA DAVID) (EVI FLIP GARY HUGO IDA) )… )

Note that we do not want permutations of the group members; i.e. ((ALDO BEAT) …) is the same solution as ((BEAT ALDO) …). However, we make a difference between ((ALDO BEAT) (CARLA DAVID) …) and ((CARLA DAVID) (ALDO BEAT) …).

You may find more about this combinatorial problem in a good book on discrete mathematics under the term “multinomial coefficients”. set-difference A B 找出A中元素不在B中的 lstA lstB 都是一个lat