Going from pseudocode to scheme -
key <-5381 each character c in w key <-33.key + ctv(c) end for
this pseudocode key function implemented in scheme life of me cant figure out how translate actual code. appreciated. , ctv function converts each character integer value ie 1 , z 26
i'll give general structure of solution, because looks homework , should try solve yourself:
(define (key str) (let loop ((chars <???>) ; transform string list of chars (acc 5381)) ; initial value of accumulator (if <???> ; if list of chars empty acc ; return accumulator (loop <???> ; otherwise advance recursion on list <???>)))) ; update accumulator, use `ctv` , apply formula
notice we're using recursion implement looping, traverse on parameter chars
(the list of characters) , use parameter acc
accumulating result returned @ end. instance, here's how value of acc
variable gets updated on successive iterations:
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
finally, don't forget test it, this:
(key "day") => 193381411
Comments
Post a Comment