CS245 Lab 4: Advanced Functional Programming

advertisement
CS245 Lab 4: Advanced Functional Programming
Due Wednesday of the 12th week of classes (23 Nov 2011); handed out on the 10th Wednesday.
Obtain the starter files for the Functional-Programming project.
1. In the file length.scm, write a scheme function list-length-fast that uses tail recursion
to compute the same result as list-length from Lab 2 (repeated in the starter files for
reference).
2. In the file shuffle.scm, write a scheme function shuffle that takes two lists and returns
a list made up of alternating elements, starting with the first element of the first list, then
the first element of the second list, then the second element of the first list, etc. For
example, (shuffle ’(1 2 3) ’(100 200 300)) should give the result (1 100 2 200 3
300). You may produce any result you feel is reasonable for lists of different lengths, but
you should describe this and anything else interesting about your function in a comment.
3. The Smalltalk system provides an operation to inject a value into a list with a given
binary function. This applies the binary function to the injected value and the first element of the list, then applies the binary function to the result it obtained and the second
element of the list, etc.. For example, (in scheme notation), (inject 0 + ’(1 2 3 4))
would produce the 10, i.e. ((((0+1)+2)+3)+4); (inject 1 * list) would produce the
product of all values in list. In the file inject.scm, write a scheme function inject that
does this.
4. Create a function injector that takes a function and an initial value and returns a new
function to inject that initial value with a given function. In other words, we could also
get 10 with the expression ((injector 0 +) ’(1 2 3 4)).
5. Modify the list-length function in length.scm to use inject instead of calling itself,
but keep a copy of the old tail-recursive definition in a comment for future reference.
6. Read Section 11.5 of the Louden text (on reserve in the Library), and the comments at the
top of lazy-lists.scm that define my lazy-list structure, and create a function lazylist-shuffle in shuffle.scm. This function should take two lazy lists and return a lazy
list of their elements shuffled, as in the regular (strict) shuffle function above.
Submit what you have at this point — you have reached the “basic credit” level;
before the end of the semester, do the “advanced” level (below) for either this
lab or the Object-Oriented AST lab.
7. Answer Louden’s Question 11.39 in the file lazy-list-same-fringe.scm. Your lazylist-flatten function should take a lazy list and return a lazy list. You may have it
make it respond to anything other than a lazy list (such as the number 17) in whatever
way you like (including by not thinking about it and letting whatever happens, happen).
Remember to submit your answers when you’re done.
Note that advanced functional programming and lazy evaluation touch on lots of potential
thesis topics in both language design and language implementation. You could probably do a
whole thesis just explaining “Transactional Haskell” well.
1
Download