f# 中的凯撒密码
我有一个项目,我必须编写凯撒密码,该密码接受字符串和移位量,然后将字符串加密为密文。我可以轻松地在 JavaScript 中完成此操作,但现在我必须在 F# 中完成此操作。也不允许循环,只允许递归。我完全感到压力和困惑,而且时间不多了,所以我在这里发帖作为最后的手段。这就是我到目前为止所拥有的一切,我觉得我正朝着完全错误的方向前进......
let rec encrypt str shiftAmount =
if str.length > 0 then
strChar = str.ToUpper().Chars(0)
strUni = int strChar
strCoded = (((strUni + shiftAmount - 65) %26) +65)
else
I have a project where I have to code a Caesar cipher that takes in a string and a shift amount and then encrypts the string into cipher text. I easily did this in JavaScript but now I have to do it in F#. Also no loops allowed only recursion. I'm completely stressed and confused and running out of time so im posting here as a last resort. This is all I have so far and I feel like I'm heading in a completely wrong direction...
let rec encrypt str shiftAmount =
if str.length > 0 then
strChar = str.ToUpper().Chars(0)
strUni = int strChar
strCoded = (((strUni + shiftAmount - 65) %26) +65)
else
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,使用映射函数(在本例中为 Array.Map())和管道而不是递归更有意义,因为您必须对中的每个字符应用一个函数(字符移位)。细绳。下面应该适用于大写字符:
可能有一个更优雅的解决方案(特别是涵盖顺时针和逆时针移位),仍在学习。
In this case it makes more sense to use a mapping function (in this case
Array.Map()
) and pipelining instead of recursion since you have to apply a function (character shifting) to each character in your string. Below should work for uppercase characters:There might be a much more elegant solution (especially to cover both clock-wise and counter-clock wise shifting), still learning myself.
这是一个天真的凯撒编码:
除此之外,我不清楚你要求什么,除了让其他人为你做所有的工作......
Here's a naive Caesar encoding:
Beyond that, it's not clear to me what you're asking for other than for someone else to do all the work for you...