#!/usr/bin/perl
#
# eg22a: 5th lazylist example, curried variation..
#

use strict;
use warnings;
use Function::Parameters;

use List;
$List::as_string_limit = 8;

# $list = power( $n, $p ) - return an infinite list n, n*p, n*p^2..
fun power( $n, $p )
{
	return List->cons( $n, fun { power($n*$p,$p); } );
}

# my $f = curry( $func, $firstarg ):
#	Partially apply (Curry) the given function $func
#	specifying it's first argument, returning a function
#	that when given the rest of it's arguments will
#	behave as the original function.
fun curry( $func, $firstarg )
{
	return fun {
		return $func->( $firstarg, @_ );
	};
}

my $twos   = power( 1, 2 );		# powers of 2
my $threes = power( 1, 3 );		# powers of 3
my $fives  = power( 1, 5 );		# powers of 5

my $numeric_merge = curry( \&merge_l, fun { $a <=> $b } );
my $m23           = $numeric_merge->( $twos, $threes );
my $m235          = $numeric_merge->( $m23, $fives );
my $all           = grep_l { $_ > 1 } $m235;
print "first few merged values: $all\n";
