I had a question about a program I've been trying to get running. Encrypt takes a message, public-key, and private-key, and returns the message with the letters from the message in the public-key changed to the letters from the private-key.
For ex, (encrypt "abcd" "abcd" "efgh") should return "efgh" and (encrypt "abcl" "abcd" "efgh") should return "efgl" (the letter from message that was not in the public-key will stay the same).
I've written a few helper programs to solve this but I keep getting the error "exception in car, __ is not a pair" when I try to run it.. but I'm not sure what's amiss. If anyone has any pointers, let me know. Thanks!
(define encrypt
(lambda (message public-key private-key)
(cond
[(list->string (encrypt-helper (string->list message)
(string->list public-key) (string->list private-key)))])))
(define encrypt-helper
(lambda (msg-ls public-ls private-ls)
(cond
[(null? public-ls) '()]
[(null? private-ls) '()]
[(and (null? public-ls) (null? private-ls)) msg-ls]
[else (cons (encrypt-key (car msg-ls) (car public-ls) (car private-ls))
(encrypt-helper (cdr msg-ls) (cdr public-ls) (cdr private-ls)))])))
;should encrypt all letters in msg-ls. not working correctly
(define encrypt-key
(lambda (char pub-key priv-key)
(cond
[(null? pub-key) char]
[(equal? char (car pub-key)) (car priv-key)]
[else (encrypt-key char (cdr pub-key) (cdr priv-key))])))
;encrypts just one letter, ex: (encrypt-key 'a '(a) '(b)) => b
;works correctly