Skip to content

Commit e830eca

Browse files
AlexKnauthsamth
authored andcommitted
Test based on question on Racket Users
1 parent 4e39ce6 commit e830eca

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

typed-racket-test/succeed/for-last.rkt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#lang typed/racket
22

3-
(require typed/rackunit)
3+
(require math/array typed/rackunit)
44

55
(check-equal? (for/last : (U String #f) ([i '(1 2 3 4 5)]
66
#:when (even? i))
@@ -23,3 +23,25 @@
2323
(list i j))
2424
(list 3 2))
2525

26+
27+
;; based on a question by 曹朝 on the Racket Users mailing list:
28+
;; https://groups.google.com/d/msg/racket-users/0tOGWZ9O57c/jRXJYkUdAQAJ
29+
(: shortest-edit-distance (-> String String (U #f Integer)))
30+
(define (shortest-edit-distance str0 str1)
31+
(let* ([l0 : Integer (string-length str0)]
32+
[l1 : Integer (string-length str1)]
33+
[table : (Mutable-Array Integer) (array->mutable-array (make-array (vector l0 l1) 0))])
34+
(for*/last : (U #f Integer) ([i0 : Integer (in-range l0)]
35+
[i1 : Integer (in-range l1)])
36+
(let* ([c0 : Char (string-ref str0 i0)]
37+
[c1 : Char (string-ref str1 i1)]
38+
[base : Integer (cond
39+
[(and (= i0 0) (= i1 0)) 0]
40+
[(= i0 0) (array-ref table (vector i0 (sub1 i1)))]
41+
[(= i1 0) (array-ref table (vector (sub1 i0) i1))]
42+
[else (min (array-ref table (vector i0 (sub1 i1)))
43+
(array-ref table (vector (sub1 i0) i1))
44+
(array-ref table (vector (sub1 i0) (sub1 i1))))])]
45+
[answer : Integer (if (char=? c0 c1) base (add1 base))])
46+
(array-set! table (vector i0 i1) answer)
47+
answer))))

0 commit comments

Comments
 (0)