#!/usr/bin/ruby

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

func lev(s, t) {
    var d = [@^(t.len+1), s.len.of{[_]}...]
    { |i|
        { |j|
            d[i+1][j+1] = (
                s[i] == t[j] ? d[i][j]
                             : [d[i][j+1], d[i+1][j], d[i][j]].min+1
              )
        } * t.len
    } * s.len
    d[-1][-1] \\ [s.len, t.len].min
}

say lev(%c'kitten', %c'sitting');               # prints: 3
say lev(%c'rosettacode', %c'raisethysword');    # prints: 8
