def mapmerge(f1, l1, f2, l2):
    """
    Helper function to merge lists of values with different map functions.
    A sorted list is returned containing f1 mapped over the elements of l1 and 
    f2 mapped over the elements ofd l2 that are not in l1; i.e. roughly:

    return sorted([ f1(i1) for i1 in l1 ] + [ f2(i2) for i2 in l2 if i2 not in l1 ])

    The actual code is a little more complex because the final sort is based on the
    original list values rather than the mapped values.
    """    
    def mm(f1, l1, f2, l2, acc):
        if len(l1) == 0: return acc + map(f2, l2)
        if len(l2) == 0: return acc + map(f1, l1)
        if l1[0] < l2[0]: return mm(f1, l1[1:], f2, l2, acc+[f1(l1[0])])
        if l1[0] > l2[0]: return mm(f1, l1, f2, l2[1:], acc+[f2(l2[0])])
        # List heads equal: choose preferentially from l1
        return mm(f1, l1[1:], f2, l2[1:], acc+[f1(l1[0])])
    return mm(f1, sorted(l1), f2, sorted(l2), [])