[taos-glug] Re: my progress report
- From: John McDermott <jjm@xxxxxxxxxx>
- To: taos-glug@xxxxxxxxxxxxx
- Date: Mon, 04 Aug 2003 16:51:50 -0600
Jonathan Bartlett wrote:
> One thing I find missing in most Scheme texts is the usefulness of being
> able to combine functions.
So (with a little help from Perlmonks as I forgot about perl's lack of
scope for nested subs) we can do this in perl with:
use strict;
my @list1 = qw/ box cow dog apple ant/;
my @list2 = (1,2,7,4,5);
sub make_counting_comparator
{
my $criterion = shift;
my $counter = 0;
return (sub {
# you could put "return" in front of the line below
$counter++; return $criterion->( $a, $b );
}, sub{$counter});
}
my @sorter1 = make_counting_comparator( sub{ $_[0] cmp $_[1] } );
my @sorter2 = make_counting_comparator( sub{ $_[0] <=> $_[1] } );
my @list = sort {$sorter1[0]()} @list1;
my $count = $sorter1[1]();
print "I sorted /@list1/ in $count steps producing /@list/\n";
my @list = sort {$sorter2[0]()} @list2;
my $count = $sorter2[1]();
print "I sorted /@list2/ in $count steps producing /@list/\n";
For me, this is far clearer. It is, however, pretty much a direct
translation. When writing this, I had initially tried to overcomplicate
it. Doing a more direct translation was far easier.
I know the point of Philip's exercise is to learn Scheme, but I like to
bend the rules a bit. Also, languages like perl seem a bit more common
for developing applications
This one makes a copy of the list instead of sorting the initial list:
that's the way perl's sort works. One could use another mechanism to
sort in place, I suppose.
When it comes to "first class"-itude, if we consider the union of
functions and function references in perl, is that union first-class?
--john
--
John McDermott
Writer, Educator, Consultant
jjm@xxxxxxxxxx http://www.jkintl.com
V +1 505/377-6293 F +1 505/377-6313
- References:
- [taos-glug] Re: my progress report
- From: Jonathan Bartlett
Other related posts:
- » [taos-glug] my progress report
- » [taos-glug] Re: my progress report
- » [taos-glug] Re: my progress report
- » [taos-glug] Re: my progress report
- » [taos-glug] Re: my progress report
- [taos-glug] Re: my progress report
- From: Jonathan Bartlett