Jump to content

Levenshtein distance: Difference between revisions

m
→‎{{header|Raku}}: no temp. variable
m (→‎{{header|PL/M}}: Removed unused declarations)
m (→‎{{header|Raku}}: no temp. variable)
Line 5,094:
=={{header|Raku}}==
(formerly Perl 6)
 
{{works with|rakudo|2015-09-16}}
Implementation of the Wikipedia algorithm. Since column 0 and row 0 are used for base distances, the original algorithm would require us to compare "@s[$i-1] eq @t[$j-1]", and reference $m and $n separately. Prepending an unused value (undef) onto @s and @t makes their indices align with the $i,$j numbering of @d, and lets us use .end instead of $m,$n.
<lang perl6>sub levenshtein-distance ( Str $s, Str $t --> Int ) {
Line 5,113:
}
 
return @d[*-1][*-1];
}
 
myfor @a = [<kitten sitting>], [<saturday sunday>], [<rosettacode raisethysword>]; -> ($s, $t) {
say "Levenshtein distance('$s', '$t') == ", levenshtein-distance($s, $t);
 
for @a -> [$s, $t] {
say "Levenshtein distance('$s', '$t') == ", levenshtein-distance($s, $t);
}</lang>
{{out}}
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.