f# 中的凯撒密码

发布于 2024-11-02 19:49:22 字数 393 浏览 1 评论 0原文

我有一个项目,我必须编写凯撒密码,该密码接受字符串和移位量,然后将字符串加密为密文。我可以轻松地在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霊感 2024-11-09 19:49:22

在这种情况下,使用映射函数(在本例中为 Array.Map())和管道而不是递归更有意义,因为您必须对中的每个字符应用一个函数(字符移位)。细绳。下面应该适用于大写字符:

let shift(c, shiftAmount) = 
     let num = int(c) - int('A')
     let offsetNum = (num+shiftAmount)%26
     let result = offsetNum + int('A')
     if offsetNum < 0 then
        char(int('Z') + offsetNum + 1)
     else
        char(offsetNum + int('A'))

let encrypt(str:string, shiftAmount) =
    str.ToCharArray() 
    |> Array.map (fun c -> shift( int(c), shiftAmount)) 
    |> String.Concat

可能有一个更优雅的解决方案(特别是涵盖顺时针和逆时针移位),仍在学习。

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:

let shift(c, shiftAmount) = 
     let num = int(c) - int('A')
     let offsetNum = (num+shiftAmount)%26
     let result = offsetNum + int('A')
     if offsetNum < 0 then
        char(int('Z') + offsetNum + 1)
     else
        char(offsetNum + int('A'))

let encrypt(str:string, shiftAmount) =
    str.ToCharArray() 
    |> Array.map (fun c -> shift( int(c), shiftAmount)) 
    |> String.Concat

There might be a much more elegant solution (especially to cover both clock-wise and counter-clock wise shifting), still learning myself.

青朷 2024-11-09 19:49:22

这是一个天真的凯撒编码:

let encode (str:string) shift =
    let inline flipzip a b = b, a
    str
    |> String.map (fun ch ->
        ch
        |> int
        |> flipzip (int 'A')
        ||> (-)
        |> (+) shift
        |> flipzip 26
        ||> (%)
        |> (+) (int 'A')
        |> char)

除此之外,我不清楚你要求什么,除了让其他人为你做所有的工作......

Here's a naive Caesar encoding:

let encode (str:string) shift =
    let inline flipzip a b = b, a
    str
    |> String.map (fun ch ->
        ch
        |> int
        |> flipzip (int 'A')
        ||> (-)
        |> (+) shift
        |> flipzip 26
        ||> (%)
        |> (+) (int 'A')
        |> char)

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文