PreviousUpNext

15.4.949  src/lib/src/list-cross-product.pkg

## list-cross-product.pkg

# Compiled by:
#     src/lib/std/standard.lib

# Functions for computing with the cross product of two lists.


###        "Future history will be a race
###         between education and catastrophe."
###
###                    -- H.G. Wells


package   list_cross_product
: (weak)  List_Cross_Product                                            # List_Cross_Product    is from   src/lib/src/list-cross-product.api
{
    # Apply a function to the cross product of two lists:
    #
    fun apply_x f (l1, l2)
        =
        lp1  l1
        where
            fun lp1 [] => ();

                lp1 (x ! r)
                    =>
                    {   fun lp2 [] => lp1 r;
                            lp2 (y ! r)
                                =>
                                {   f (x, y);
                                    lp2 r;
                                };
                        end;
                
                        lp2 l2;
                    };
            end;
        end;


    # Map a function across the cross product of two lists:
    #
    fun map_x f (l1, l2)
        =
        {   fun lp1 ([], result_l)
                    =>
                    reverse result_l;

                lp1 (x ! r, result_l)
                    =>
                    {   fun lp2 ([], result_l)
                                =>
                                lp1 (r, result_l);

                            lp2 (y ! r, result_l)
                                =>
                                lp2 (r, f (x, y) ! result_l);
                        end;
                
                        lp2 (l2, result_l);
                    };
            end;
          
            lp1 (l1, []);
        };


    # Fold a function across the cross product of two lists:
    #
    fun fold_x f (l1, l2)
        =
        {   fun lp1 ([], accum)
                    =>
                    accum;

                lp1 (x ! r, accum)
                    =>
                    {   fun lp2 ([],    accum) =>  lp1 (r, accum);
                            lp2 (y ! r, accum) =>  lp2 (r, f (x, y, accum));
                        end;
                
                        lp2 (l2, accum);
                    };
            end;
          
            \\ init =  lp1 (l1, init);
        };

};                                              # package list_cross_product 


## COPYRIGHT (c) 1993 by AT&T Bell Laboratories.  See SMLNJ-COPYRIGHT file for details.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext