RU beehive logo ITEC dept promo banner
ITEC 380
2008fall
ibarland

homeinfolecturesexamsarchive

lect05a
mutual recursion; map/filter/fold

Review: mutual-recursion in lect04c.ss Future comment: filter -- eg facebook when you becomes friends with X, and it gives a list of 'suggest to X as possible known friends' You need to remove the ones who are already friends (or, keep only those who share a network with you and X):
(filter (lambda (f) (not are-friends(X,f)))
        (user-friends Ian))
In Java/OO:
  Set soFar = new HashSet();
  for ( User f : ian.getFriends() ) {
    if (f.isFriendOf(X)) {
      soFar.add(f);
    }
  }
  return soFar;
Or: keep only those who share a network with you and X, and aren't already friends:
  (filter (lambda (f)
             (and (not (empty? (intersect (user-groups f) (user-groups X))))
                  (not (are-friends f X)))
          (user-friends ian))
  Set findPertinentFriends( User X ) {
    Set soFar = new HashSet();
    for ( User f : ian.getFriends() ) {
      if (!f.getGroups().intersect( g.getGroups ).isEmpty()
          && !f.isFriend(X)) {
         soFar.add(f);
      }
    }
    return soFar;
  }

  ...
  ian.findPertinentFriends(X)
Note how in the second example, the difference isn't as big; Java has already invested in its loop overhead. (Should nested-(filter/maps) be needed, this re-appears. Differences/similarities: Pros of OO (vs functional) - if swap out better implementations of Set (w/ faster 'intersect'), don't need to change this code. - we know where to locate the code : look in class Friend. Cons of OO: - we *must* locate this code in class Friend (which is getting huge) (The Pros heavily outweight the cons) ---- on more an issue of pragmatics, than paradigm: Cons of Java (vs Scheme) - can't cut/paste; *must* write as separate method - Uses 7 syntax rules, for no particular gain Note that there is *not* a contradiction between functional and OO; you can do both. Similarly, you can do OO-in-scheme and you can do functional-in-Java(*). (*) Actually, Java limits the call-stack to 6000 frames or so by default, which can cause problems with some OO solutions -- processing a list with 100,000 items in a way which isn't tail-recursive. In scheme, you'd probably write a tail-recursive version, but the JVM doesn't support tail-recursion optimation.

homeinfolecturesexamsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Oct.10 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme