Created
October 30, 2014 12:19
-
-
Save a13x/1044a9ef1b9904db7b9d to your computer and use it in GitHub Desktop.
Dice coeficient in Scheme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/gsi-script | |
(define (bigram lst) | |
(cons (car lst) (cadr lst))) | |
(define (bigrams lst) | |
(if (> (length lst) 2) | |
(cons (bigram lst) (bigrams (cdr lst))) | |
(cons (bigram lst) '()))) | |
(define (match ls1 ls2) | |
(let ((nmatch 0)) | |
(for-each | |
(lambda (x) | |
(if (member x ls2) | |
(set! nmatch (+ nmatch 1)) | |
)) | |
ls1) | |
nmatch)) | |
(define (dicecoef ls1 ls2) | |
(let ([bls1 (bigrams ls1)] | |
[bls2 (bigrams ls2)] | |
) | |
(/ (* 2 (match bls1 bls2)) (+ (length bls1) (length bls2))))) | |
(define (main . args) | |
(define (usage) (display "usage: dicecoef <word> <word>")) | |
(if (or (null? args) (not (= (length args) 2))) | |
(usage) | |
(let ((w1 (car args)) (w2 (cadr args))) | |
(display (string-append | |
"Dice coeficient of " w1 " and " w2 " is: ")) | |
(display (exact->inexact (dicecoef | |
(string->list w1) | |
(string->list w2)))) | |
(newline)) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment