w 中每个字符 c 的 key <-5381 do key <-33.key + ctv(c) end for
这是要在方案中实现的关键功能的伪代码,但在我的一生中,我无法弄清楚如何将其转换为实际代码。任何帮助,将不胜感激。ctv 是将每个字符转换为其整数值的函数,即 a 为 1,z 为 26
w 中每个字符 c 的 key <-5381 do key <-33.key + ctv(c) end for
这是要在方案中实现的关键功能的伪代码,但在我的一生中,我无法弄清楚如何将其转换为实际代码。任何帮助,将不胜感激。ctv 是将每个字符转换为其整数值的函数,即 a 为 1,z 为 26
我只会给你解决方案的一般结构,因为这看起来像家庭作业,你应该尝试自己解决它:
(define (key str)
(let loop ((chars <???>) ; transform the string into a list of chars
(acc 5381)) ; this is the initial value of the accumulator
(if <???> ; if the list of chars is empty
acc ; then return the accumulator
(loop <???> ; otherwise advance recursion over list
<???>)))) ; update accumulator, use `ctv` and apply the formula
请注意,我们使用递归来实现循环,我们遍历参数chars
(字符列表)并使用参数acc
来累积将在最后返回的结果。例如,以下是acc
变量的值如何在连续迭代中更新:
acc: 5381 ; initial value, acc = 5381
acc: 33 * acc + ctv(d) ; processing 'd', acc = 177577
acc: 33 * acc + ctv(a) ; processing 'a', acc = 5860042
acc: 33 * acc + ctv(y) ; processing 'y', acc = 193381411
最后别忘了测试一下,像这样:
(key "day")
=> 193381411