Sie sind auf Seite 1von 1

Lab 7

IMPORTANT NOTE:
The template for processing a list:
(define (fn-list-name alist)
(cond
[(empty? alist) ]
[else ... (first alist) ...(fn-list-name (rest alist)) ...]))

1. Write a Scheme function bigger-x that consumes a list of posns and produces the number of
posns in the list which have their x-coordinate greater than or equal to their y-coordinate.

2. Create a function count-even-strings that consumes a list of strings and produces the number
of strings in the list that have even length.
Hint: The function consumes a list of string, and produces number of strings in the list that
have even length Required string function: string-length, primitive function: even?

3. Create a function list-pos that consumes a nonempty list and an item that is guaranteed to be
in the list and returns the position of the first occurrence of the item in the list. The first item
in the list is in position 0.
Hint: For this question, the template shown above need to be modified as we are guaranteed
that the list will NOT be empty a nonempty list. Therefore the base case testing is not
empty. The parametric item is guaranteed to be in the list.
So, we check for the first element in the list for item, if found, returns 0.
We have not decided what to do with the else clause yet...
[else ... (first alist) ...(list-pos name (rest alist)) ...]))
Now, if it is not found in the first item, then we recursively search for the item in the list. But
of course, we need to add 1 to the position as we moved to the rest of the list.

4. Create a function differences that consumes a nonempty list of numbers and produces a list
of differences between adjacent pairs (or empty for a list of length 1). For example, for the
list 25, 16, 9, 1, 4 the list of differences would be 9, 7, 8, -3.
Hint: Again, we are told that the list is a nonempty list of numbers, so the base-case testing
of empty is not applicable here. The testing is for the list length to be 1 if true, we return
empty as there is no difference of a single number. If it is more than 1 number, that is, at
least two numbers, then what we do is to construct a list of the difference between the first
two numbers, and recursively call to find the difference of the rest of the list.

5. Create a function next-list that consumes a list and an item, and produces either the item in
the list that appears after the input item or the symbol none if the input item is either the last
item in the list or not in the list.
Hint: There are two base cases here:
Case 1: If the list is empty, then we are done and we return the symbol none.
Case 2: If the list has only 1 item, then definitely there will not be any item after it, i.e.
there is no successor, so we return none as well.
But if the list contains at least two items, then we may or may have the item that we want to
search for. Since there are alternatives, therefore we use cond again. The first case is that if
the first item is the same as the parametric item, then we done. And we return the next item
that comes after the parametric (or first) item (i.e. the second item in the list). If the first item
is not the same, then we recursively check for the item in the rest of the list.

Das könnte Ihnen auch gefallen