在ocaml中进行凯撒密码检查 [英] caesar cipher check in ocaml

查看:80
本文介绍了在ocaml中进行凯撒密码检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个给定两个字符串s1s2的检查功能,该功能将检查s2是否为caesar cipher of s1.界面必须看起来像string->string->bool.

问题是我不允许使用除String.length之外的任何字符串函数,那么我该如何解决呢?我不允许任何list arrayiterations.仅recursionspattern matching.

请帮助我.而且您还能告诉我如何在具有上述限制的模块函数之外的ocaml中编写子字符串函数吗?

解决方案

我的猜测是,可能允许您使用s.[i]来获取字符串s的第i个字符.这与String.get相同,但是讲师可能不会想到这些术语.如果没有某种形式获取字符串的各个字符,我相信这是不可能的.您可能应该与您的讲师再确认一次,但是,如果他的意思是您无法将字符串分隔为字符(这在Ocaml中单独使用模式匹配是无法做到的),我会感到惊讶. /p>

一旦您可以获得单个字符,它的方法应该很清楚(您不需要子字符串来递归地遍历每个字符串).

如果您仍然想编写子字符串,由于无法访问String.create或其他类似函数,因此创建子字符串会很复杂.但是您可以使用递归,一个字符串文字(例如"x"),将字符串中的字符设置为另一个字符(例如s.[0] <- c)以及字符串串联(s1 ^ s2)来编写自己的String.create版本. ).再次,当然,所有这些都是在假设允许使用这些运算符的情况下进行的.

I want to implement a check function that given two strings s1 and s2 will check if s2 is the caesar cipher of s1 or not. the inter face needs to be looked like string->string->bool.

the problem is that I am not allowed to use any string functions other than String.length, so how can I solve it? i am not permitted any list array, iterations. Only recursions and pattern matching.

Please help me. And also can you tell me how I can write a substring function in ocaml other than the module function with the above restrictions?

解决方案

My guess is that you are probably allowed to use s.[i] to get the ith character of string s. This is the same as String.get, but the instructor may not think of it in those terms. Without some form of getting the individual characters for the string, I believe that this is impossible. You should probably double check with your instructor to be sure, but I would be surprised if he had meant for you to be unable to separate a string into characters (which is something that you cannot do with pattern-matching alone in Ocaml).

Once you can get individual characters, the way to do it should be pretty clear (you do not need substring to traverse each string recursively).

If you still want to write substring, creating it would be complex since you don't have access to String.create or other similar functions. But you can write your own version of String.create using recursion, one character string literals (like "x"), the ability to set a character in a string to another (like s.[0] <- c), and string concatenation (s1 ^ s2). Again, of course, all of this is assuming that those operators are allowed to be used.

这篇关于在ocaml中进行凯撒密码检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆