2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,44 @@
|
|||
-- <= for lists
|
||||
-- compare :: [a] -> [a] -> Bool
|
||||
on compare(xs, ys)
|
||||
if length of xs = 0 then
|
||||
true
|
||||
else
|
||||
if length of ys = 0 then
|
||||
false
|
||||
else
|
||||
set {hx, txs} to uncons(xs)
|
||||
set {hy, tys} to uncons(ys)
|
||||
|
||||
if hx = hy then
|
||||
compare(txs, tys)
|
||||
else
|
||||
hx < hy
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
end compare
|
||||
|
||||
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
|
||||
{compare([1, 2, 1, 3, 2], [1, 2, 0, 4, 4, 0, 0, 0]), ¬
|
||||
compare([1, 2, 0, 4, 4, 0, 0, 0], [1, 2, 1, 3, 2])}
|
||||
|
||||
end run
|
||||
|
||||
|
||||
---------------------------------------------------------------------------
|
||||
|
||||
-- GENERIC FUNCTION
|
||||
|
||||
-- uncons :: [a] -> Maybe (a, [a])
|
||||
on uncons(xs)
|
||||
if length of xs > 0 then
|
||||
{item 1 of xs, rest of xs}
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end uncons
|
||||
|
|
@ -0,0 +1 @@
|
|||
{false, true}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
iex(1)> [1,2,3] < [1,2,3,4]
|
||||
true
|
||||
iex(2)> [1,2,3] < [1,2,4]
|
||||
true
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// <= is already defined for lists in JS
|
||||
|
||||
// compare :: [a] -> [a] -> Bool
|
||||
const compare = (xs, ys) => xs <= ys;
|
||||
|
||||
|
||||
// TEST
|
||||
return [
|
||||
compare([1, 2, 1, 3, 2], [1, 2, 0, 4, 4, 0, 0, 0]),
|
||||
compare([1, 2, 0, 4, 4, 0, 0, 0], [1, 2, 1, 3, 2])
|
||||
];
|
||||
|
||||
// --> [false, true]
|
||||
})()
|
||||
|
|
@ -0,0 +1 @@
|
|||
[false, true]
|
||||
|
|
@ -11,7 +11,7 @@ say @a," before ",@b," = ", @a before @b;
|
|||
say @a," before ",@b," = ", @a before @b;
|
||||
|
||||
for 1..10 {
|
||||
my @a = (^100).roll((2..3).pick);
|
||||
my @b = @a.map: { Bool.pick ?? $_ !! (^100).roll((0..2).pick) }
|
||||
my @a = flat (^100).roll((2..3).pick);
|
||||
my @b = flat @a.map: { Bool.pick ?? $_ !! (^100).roll((0..2).pick) }
|
||||
say @a," before ",@b," = ", @a before @b;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,36 +1,38 @@
|
|||
#!/usr/bin/perl -w
|
||||
use strict ;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub orderlists {
|
||||
my $firstlist = shift ;
|
||||
my $secondlist = shift ;
|
||||
my $first = shift @{$firstlist } if @{$firstlist} ;
|
||||
my $second ;
|
||||
#keep stripping elements from the first list as long as there are any
|
||||
#or until the second list is used up!
|
||||
while ( @{$firstlist} ) {
|
||||
if ( @{$secondlist} ) { #second list is not used up yet!
|
||||
$second = shift @{$secondlist} ;
|
||||
if ( $first < $second ) {
|
||||
return 1 ;
|
||||
}
|
||||
if ( $first > $second ) {
|
||||
return 0 ;
|
||||
}
|
||||
}
|
||||
else { #second list used up, defined to return false
|
||||
return 0 ;
|
||||
}
|
||||
$first = shift @{$firstlist} ;
|
||||
}
|
||||
return 0 ; #in all remaining cases return false
|
||||
my ($firstlist, $secondlist) = @_;
|
||||
|
||||
my ($first, $second);
|
||||
while (@{$firstlist}) {
|
||||
$first = shift @{$firstlist};
|
||||
if (@{$secondlist}) {
|
||||
$second = shift @{$secondlist};
|
||||
if ($first < $second) {
|
||||
return 1;
|
||||
}
|
||||
if ($first > $second) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@{$secondlist} ? 1 : 0;
|
||||
}
|
||||
|
||||
my @firstnumbers = ( 43 , 33 , 2 ) ;
|
||||
my @secondnumbers = ( 45 ) ;
|
||||
if ( orderlists( \@firstnumbers , \@secondnumbers ) ) {
|
||||
print "The first list comes before the second list!\n" ;
|
||||
}
|
||||
else {
|
||||
print "The first list does not come before the second list!\n" ;
|
||||
foreach my $pair (
|
||||
[[1, 2, 4], [1, 2, 4]],
|
||||
[[1, 2, 4], [1, 2, ]],
|
||||
[[1, 2, ], [1, 2, 4]],
|
||||
[[55,53,1], [55,62,83]],
|
||||
[[20,40,51],[20,17,78,34]],
|
||||
) {
|
||||
my $first = $pair->[0];
|
||||
my $second = $pair->[1];
|
||||
my $before = orderlists([@$first], [@$second]) ? 'true' : 'false';
|
||||
print "(@$first) comes before (@$second) : $before\n";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
function order($as,$bs) {
|
||||
if($as -and $bs) {
|
||||
$a, $as = $as
|
||||
$b, $bs = $bs
|
||||
if($a -eq $b) {order $as $bs}
|
||||
else{$a -lt $b}
|
||||
} elseif ($bs) {$true} else {$false}
|
||||
}
|
||||
"$(order @(1,2,1,3,2) @(1,2,0,4,4,0,0,0))"
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
function Test-Order ([int[]]$ReferenceArray, [int[]]$DifferenceArray)
|
||||
{
|
||||
for ($i = 0; $i -lt $ReferenceArray.Count; $i++)
|
||||
{
|
||||
if ($ReferenceArray[$i] -lt $DifferenceArray[$i])
|
||||
{
|
||||
return $true
|
||||
}
|
||||
elseif ($ReferenceArray[$i] -gt $DifferenceArray[$i])
|
||||
{
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
return ($ReferenceArray.Count -lt $DifferenceArray.Count) -or (Compare-Object $ReferenceArray $DifferenceArray) -eq $null
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Test-Order -ReferenceArray 1, 2, 1, 3, 2 -DifferenceArray 1, 2, 0, 4, 4, 0, 0, 0
|
||||
Test-Order -ReferenceArray 1, 2, 1, 3, 2 -DifferenceArray 1, 2, 2, 4, 4, 0, 0, 0
|
||||
Test-Order -ReferenceArray 1, 2, 3 -DifferenceArray 1, 2
|
||||
Test-Order -ReferenceArray 1, 2 -DifferenceArray 1, 2, 3
|
||||
Test-Order -ReferenceArray 1, 2 -DifferenceArray 1, 2
|
||||
Loading…
Add table
Add a link
Reference in a new issue