Exercises in this lecture  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Go to the slide, where this exercise belongs -- Keyboard shortcut: 'u'  

Exercise 1.11
A counterpart to list-tail **


The Scheme function list-tail returns the n'th tail of a list:

  > (list-tail '(a b c d e)  2)
 (c d e)
  > (list-tail '(a b c d e)  0)
 (a b c d e)
 > (list-tail '(a b c d e)  4)
 (e)

Program your own version of list-tail. Call it my-list-tail.

Next, program a function list-prefix which is a prefix counterpart to list-tail. (list-prefix lst n) returns a prefix of the list of length n:

 > (list-prefix '(a b c d e)  2)
 (a b)
 > (list-prefix '(a b c d e)  4)
 (a b c d)
 > (list-prefix '(a b c d e)  0)
 () 

Do not just use reverse and/or length in clever ways. Make 'a real implementation' of list-prefix.

Reflect on the difference between these two functions. Which one is most expensive in terms of memory allocation?


Solution