Based on your latest string-scorer
, I would do the following:
(define (char-scorer char1 char2)
(cond
((char=? char1 char2) 2)
((or (char=? #\_ char1) (char=? #\_ char2)) -2)
(else -1)))
(define (alignment-score-not-tail string1 string2)
(apply + (map char-scorer (string->list string1) (string->list string2))))
then
> (alignment-score-not-tail "Hello" "__low")
-4
Some details:
- I chose to compare chars, not one-letter strings. This comes in handy later when using
string->list
. Hence also the use ofchar=?
which is more specific thanequal?
. - I convert the strings to lists, for example
(string->list "Hello")
is'(#\H #\e #\l #\l #\o)
. - If you
map
your char comparison to the 2 strings converted to lists, you get a list with the individual results (in your example,'(-2 -2 2 -1 -1)
. - Now you just have to add all these numbers, that's what
apply +
does. Not the best solution, but works for small lists.
Alternatively, if this is supposed to be a non tail-recursive procedure, you could do the following:
(define (alignment-score-not-tail string1 string2)
(define (helper lst1 lst2)
(if (or (null? lst1) (null? lst2))
0
(+ (char-scorer (car lst1) (car lst2)) (helper (cdr lst1) (cdr lst2)))))
(helper (string->list string1) (string->list string2)))
and transforming to a tail-recusive one could be done as follows:
(define (alignment-score-tail string1 string2)
(define (helper lst1 lst2 result)
(if (or (null? lst1) (null? lst2))
result
(helper (cdr lst1) (cdr lst2) (+ result (char-scorer (car lst1) (car lst2))))))
(helper (string->list string1) (string->list string2) 0))