#!/usr/bin/ruby

# http://rosettacode.org/wiki/Levenshtein_distance#Sidef

func lev(s, t) {
    var d = [@(0 .. t.len), s.len.of {[_]}...]
    for i,j in (^s ~X ^t) {
        d[i+1][j+1] = (
            s[i] == t[j]
                ? d[i][j]
                : 1+Math.min(d[i][j+1], d[i+1][j], d[i][j])
        )
    }
    d[-1][-1]
}

assert_eq(lev(%c'kitten', %c'sitting'), 3)
assert_eq(lev(%c'rosettacode', %c'raisethysword'), 8)

say "** Test passed!";
