Com S 342 meeting -*- Outline -*- * sequences as conventional interfaces (2.2.3) ** motivational example --------------------------------------------------------- FOR YOU TO DO (sum-odd-squares (list)) ==> 0 (sum-odd-squares (list 4 2 5)) ==> 25 (sum-odd-squares (list 3 4 2 5)) ==> 34 (sum-odd-squares (list 3 (list (list 52 48)))) ==> 9 (sum-odd-squares (list (list 52 (list 1 48) 2) 5)) ==> 26 (even-fibs 1) ==> (0) (even-fibs 2) ==> (0) (even-fibs 3) ==> (0 2) (even-fibs 4) ==> (0 2) (even-fibs 5) ==> (0 2) (even-fibs 6) ==> (0 2 8) (even-fibs 10) ==> (0 2 8 34) (even-fibs 20) ==> (0 2 8 34 144 610 2584) --------------------------------------------------------- Q: what do these two have in common? Draw the diagram of these as pipelines ____________ _______ ______ ___________ tree ->|enumerate |->|filter |->|map |->|accumulate | -> list |tree leaves| |odd? | |square| |plus, 0 | |___________| |_______| |______| |___________| ____________ _______ ______ ___________ number ->|enumerate |->|map |->|filter|->|accumulate | -> list |integers | |fib | |even? | |cons, nil | |___________| |_______| |______| |___________| Q: How could we make this into Scheme code? Q: What procedures do we need to write? Have them write filter, accumulate, and enumerate-interval, and enumerate-tree Q: Now can you put these together to write sum-odd-squares, and even-fibs? ** more examples Q: Can you make the procedure salary-of-highest-paid-programmer, assuming we are given a procedure salary and a predicate programmer? ** language design Q: can we make something that looks even more like the diagrams? Hint: recall that compose works from right to left as well, Q: can you write a version of compose works in the opposite order? Q: how could this help us solve the original problem? Hint: would it help to curry any of the procedures? ** architecture it is often convenient to think of functional programs as having a pipeline architecture, like those above. Q: what were the stages or transformations in the pipeline? Map, filter, accumulate Q: what type of information is passed between the different stages of the pipeline? So sequences (lists) are the "conventional" interfaces between different stages of these pipelines Q: what's the advantage of this for the users? According to Waters, 90 percent of the code in the Fortran scientific subroutine package can be expressed in terms of maps, filters, and accumulations. ** in Java show how to do this in Java and two ways: 1. First as a direct transliteration of the Scheme code (in $PUB/lib/Cons.java) 2. Second, using iterators package lib; public class Map implements Iterator { protected Function saved_proc; protected Iterator saved_seq; /*@ public invariant: saved_proc != null && saved_seq != null @*/ public Map(Function proc, Iterator seq) //@ requires: proc != null && seq != null; { saved_proc = proc; saved_seq = seq; } public boolean hasMore() { return saved_seq.hasMore(); } public void advance() { saved_seq.advance(); } public Object getElement() { return saved_proc.value(saved_seq.getElement()); } } Q: Can you do Filter like this? (see $PUB/lib/Filter.java)