Ejercicios de Scheme

2
GUIA DE EJERCICIOS DE SCHEME Junio del 2003 1. Define a Scheme procedure attach-at-end that takes any value as its first argument and a list as its second argument and returns a list containing all of the same elements as the given list except that the given value has been added as the last element. 2. Define a Scheme procedure concatenate that takes any two lists as arguments and returns a single list containing all of the elements of the first list, in order, followed by all of the elements of the second list, in order. (Scheme already has a built-in procedure called append that does this. Don't use it.) 3. Define a Scheme procedure position that takes any value as its first argument and a list as its second argument and returns the number of elements of the given list that precede the first occurrence of the given value, or #f if the given value does not occur at all on the given list. 4. Define a Scheme procedure mean that takes a non-empty list of numbers and returns the arithmetic mean of its elements. 5. Adapt the mean procedure so that it takes any list as an argument and returns the arithmetic mean of just the elements that are numbers, ignoring the rest. It should call error if the list contains no numbers. 6. Define a Scheme procedure iota that takes a natural number and produces a list, in ascending order, of all the smaller natural numbers. For instance, the value of (iota 5) should be (0 1 2 3 4); the value of (iota 1) should be (0); and the value of (iota 0) should be the empty list. 7. Define a Scheme procedure quadratic that takes three arguments a, b, and c, all real numbers, and returns a list of the roots of the quadratic equation ax^2 + bx + c = 0. 8. Define a Scheme procedure subst that takes three arguments (two arbitrary values and a list) and returns a list just like the given list except that every occurrence of the first value has been replaced with the second value. For instance, the value of the call (subst 'c 'k '(c o c o n u t)) should be (k o k o n u t). 9. Define a Scheme procedure list-segment that takes three arguments -- the first a list, the second a natural number no greater than the length of the list, and the third a natural number no less than the second argument but no greater than the length of the list -- and returns a segment of the given list, starting after the number of elements indicated by the second argument and stopping after the element indicated by the third argument. For instance, the value of the call (list-segment '(a b c d e f g) 3 5) should be (d e) -- the segment of (a b c d e f g) that starts after the third element and stops at the fifth. If the second and third elements are equal, list-segment should return the empty list. 10. Define a Scheme procedure range-sum that takes two integer arguments and finds the sum of all the integers greater than or equal to the first argument and less than or equal to the second. For instance, the value of the call (range-sum 12 16) should be 70 (that is, 12 + 13 + 14 + 15 + 16). 11. Let's define a numeric structure, recursively, as a value that is either the null object, or a real number, or a pair of numeric structures. Define a Scheme procedure that finds the sum of all the real numbers that occur anywhere inside a given numeric structure. 12. Define a Scheme procedure that finds that greatest real number in a given numeric structure. (If the structure contains no real numbers -- for instance, if it is the null object -- the procedure should return #f.) 13. Similarly, let's define a symbolic structure as a value that is either the null object, or a symbol, or a pair of symbolic structures. Define a Scheme procedure contents that takes any symbolic structure as its argument and returns a list of all the symbols that occur anywhere inside that symbolic structure.

description

Ejercicios de scheme resueltos

Transcript of Ejercicios de Scheme

Page 1: Ejercicios de Scheme

GUIA DE EJERCICIOS DE SCHEME Junio del 2003

1. Define a Scheme procedure attach-at-end that takes any value as its first argument and a list as its second argument and returns a list containing all of the same elements as the given list except that the given value has been added as the last element. 2. Define a Scheme procedure concatenate that takes any two lists as arguments and returns a single list containing all of the elements of the first list, in order, followed by all of the elements of the second list, in order. (Scheme already has a built-in procedure called append that does this. Don't use it.) 3. Define a Scheme procedure position that takes any value as its first argument and a list as its second argument and returns the number of elements of the given list that precede the first occurrence of the given value, or #f if the given value does not occur at all on the given list. 4. Define a Scheme procedure mean that takes a non-empty list of numbers and returns the arithmetic mean of its elements. 5. Adapt the mean procedure so that it takes any list as an argument and returns the arithmetic mean of just the elements that are numbers, ignoring the rest. It should call error if the list contains no numbers. 6. Define a Scheme procedure iota that takes a natural number and produces a list, in ascending order, of all the smaller natural numbers. For instance, the value of (iota 5) should be (0 1 2 3 4); the value of (iota 1) should be (0); and the value of (iota 0) should be the empty list. 7. Define a Scheme procedure quadratic that takes three arguments a, b, and c, all real numbers, and returns a list of the roots of the quadratic equation ax^2 + bx + c = 0. 8. Define a Scheme procedure subst that takes three arguments (two arbitrary values and a list) and returns a list just like the given list except that every occurrence of the first value has been replaced with the second value. For instance, the value of the call (subst 'c 'k '(c o c o n u t)) should be (k o k o n u t). 9. Define a Scheme procedure list-segment that takes three arguments -- the first a list, the second a natural number no greater than the length of the list, and the third a natural number no less than the second argument but no greater than the length of the list -- and returns a segment of the given list, starting after the number of elements indicated by the second argument and stopping after the element indicated by the third argument. For instance, the value of the call (list-segment '(a b c d e f g) 3 5) should be (d e) -- the segment of (a b c d e f g) that starts after the third element and stops at the fifth. If the second and third elements are equal, list-segment should return the empty list. 10. Define a Scheme procedure range-sum that takes two integer arguments and finds the sum of all the integers greater than or equal to the first argument and less than or equal to the second. For instance, the value of the call (range-sum 12 16) should be 70 (that is, 12 + 13 + 14 + 15 + 16). 11. Let's define a numeric structure, recursively, as a value that is either the null object, or a real number, or a pair of numeric structures. Define a Scheme procedure that finds the sum of all the real numbers that occur anywhere inside a given numeric structure. 12. Define a Scheme procedure that finds that greatest real number in a given numeric structure. (If the structure contains no real numbers -- for instance, if it is the null object -- the procedure should return #f.) 13. Similarly, let's define a symbolic structure as a value that is either the null object, or a symbol, or a pair of symbolic structures. Define a Scheme procedure contents that takes any symbolic structure as its argument and returns a list of all the symbols that occur anywhere inside that symbolic structure.

Page 2: Ejercicios de Scheme

14. Define the iota procedure (see definition above) so that it obtains its result by an iterative process. 15. Define a Scheme predicate that tests whether a given list is palindromic -- whether its first element is equal? to its last, its second to its next-to-last, and so on. 16. Define a Scheme procedure display-structure that uses display to print out a representation of any symbolic structure, as defined above. It should use the following representation conventions:

• The null object should be displayed as #n. • A symbol should be displayed as itself; for instance, the symbol alpha should be displayed

as alpha. • A pair should be displayed as a left parenthesis, the displayed version of its left field, a

space, a dot, another space, the displayed version of its right field, and a right parenthesis. Use recursive calls to display-structure to print the left and right fields.

Don't care what value display-structure returns. #<unspecified> would be fine. 17. Define a Scheme procedure filter that takes two arguments, a predicate and a list, and returns a list that contains just the elements of the given list that satisfy the given predicate (that is, the elements for which applying the predicate to the element would return #t). For instance, (filter odd? '(3 8 6 14 9 2)) should return (3 1 9). 18. Define a Scheme procedure merge that takes two lists of real numbers, both in ascending order, and returns a single list, containing all the elements from both lists, also in ascending order. 19 Suppose we represent a point in the Cartesian plane by a pair of real numbers, the car representing the point's x-coordinate and the cdr its y coordinate. Suppose further that we represent a line segment by a pair of which the two fields are the two endpoints of the segment. Define a Scheme predicate that takes two line segments, one horizontal (i.e., both of its endpoints have the same y-coordinate) and the other vertical, and determines whether they intersect (that is, whether they have t least one point in common). 20. Matrix can be represented in Scheme by a vector of vectors, each of the element vectors constituting one row of the matrix. Define a Scheme procedure identity that takes a positive integer n as its argument and returns an n-by-n identity matrix.