加密【Petite Chez 方案】
我对我一直试图运行的程序有疑问。 加密采用消息、公钥和私钥,并返回消息,并将公钥中消息中的字母更改为私钥中的字母。
例如,(加密“abcd”“abcd”“efgh”)应返回“efgh” 并且(加密“abcl”“abcd”“efgh”)应该返回“efgl”(不在公钥中的消息中的字母将保持不变)。
我已经编写了一些帮助程序来解决这个问题,但是当我尝试运行它时,我不断收到错误“汽车异常,__ 不是一对”。但我不确定出了什么问题。如果有人有任何指示,请告诉我。谢谢!
(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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是在
encrypt-helper
内部,您正在调用(car public-ls)
(和(car private-ls)
)是一个原子,而在encrypt-key
内部你也可以执行,但你不能在这里
car pub-key
因为car
只适用于列表,而 pub-key 是一个原子。在您给出的示例中,
您会注意到
'(a)
和'(b)
被指定为列表,正是出于这个原因。提示:我会把它留在那里:)
The problem is that inside
encrypt-helper
, you're callingbut
(car public-ls)
(and(car private-ls)
) is an atom, while insideencrypt-key
you also performand you can't
car pub-key
here becausecar
only works on a list, whilepub-key
is an atom.In the example you give that works, i.e.
you'll notice that
'(a)
and'(b)
are specified as lists, for exactly this reason. Hint:I'll leave it there :)