We will write a procedure every-nth that accepts two arguments: num is an integer and list-of-sents is a list of a certain number of lists. every-nth selects the num-th elements from each list in list-of-sents and returns the list of selected elements. Assume that all lists in listof-sents have more than num elements. Consider the first element as the 0-th. Examples: (every-nth 2 '((a b c d) (e f g h))) gives (c g). (every-nth 1 '((a b c) (d e f) (g h i))) gives (b e h). (define (every-nth num list-of-sents) ; You must fill in this part )
时间: 2023-12-03 19:47:08 浏览: 95
Here's one way to implement the `every-nth` procedure:
```
(define (every-nth num list-of-sents)
(apply map
(lambda args (list-ref args num))
list-of-sents))
```
In this implementation, we use `map` to apply a function to each element of `list-of-sents`. The function we apply is a lambda that takes `num` arguments (one from each sublist) and returns the `num`-th element of the sublist. We use `apply` to pass the sublists as separate arguments to `map`.
Note that we assume that all sublists have at least `num+1` elements, since we count the first element as the 0-th. If some sublists might have fewer elements, we should add a check to avoid an index out of bounds error.
阅读全文