【发布时间】:2014-03-31 14:22:56
【问题描述】:
我对函数式编程很陌生,尤其是下面使用的 Scheme。我正在尝试制作以下递归的尾递归函数。 基本上,该函数的作用是对两个字符串的对齐进行评分。当给定两个字符串作为输入时,它会比较每个“列”字符并根据在名为 scorer 的函数中实现的评分方案为该对齐累积分数,该函数由下面代码中的函数调用。
我有点想使用辅助函数来累积分数,但我不太确定如何做到这一点,因此我将如何使函数低于尾递归?
(define (alignment-score string_one string_two)
(if (and (not (= (string-length string_one) 0))
(not (=(string-length string_two) 0)))
(+ (scorer (string-ref string_one 0)
(string-ref string_two 0))
(alignment-score-not-tail
(substring string_one 1 (string-length string_one))
(substring string_two 1 (string-length string_two))
)
)
0)
)
【问题讨论】:
标签: recursion functional-programming tail