简体   繁体   English

OCaml中的字符串操作

[英]Operations on string in OCaml

I want to write these functions in pure OCaml 我想用纯OCaml编写这些函数

Length : that calculate the length of a given string 长度:计算给定字符串的长度

getChar : the same as String.get that return the char positioned at the given index. getChar:与String.get相同,后者返回位于给定索引处的char。

I can't use any function that figure in String module or in other modules. 我不能在String模块或其他模块中使用任何该数字的函数。 Is string are list of char like in other language. 字符串就像其他语言一样是char的列表。 Or how to transform a string to a list of char? 或者如何将字符串转换为char列表?

Thanks for your help. 谢谢你的帮助。

The length of a value of type string cannot be obtained without String.length as far as I know. 据我所知,没有String.length不能获得string类型的值的长度。 Well, you could write a loop that tries to read all characters until there is an exception, but I think it is rather ugly. 好吧,您可以编写一个循环,尝试读取所有字符,直到出现异常为止,但我认为这很丑陋。

Regarding getChar I don't think it can be done either, unless you allow yourself to use the normal way to read a string: s.[i]. 关于getChar,我也不认为这也是可以完成的,除非您允许自己使用正常的方式读取字符串:s。[i]。

If you can't use anything but OCaml primitives, my guess is that you have to define your own string type, perhaps as a char list as you suggest: 如果除了OCaml原语之外什么都不能使用,我的猜测是您必须定义自己的字符串类型,也许像建议的那样作为一个字符列表:

type my_string = char list 输入my_string =字符列表

Then, just re-define List.length and List.nth. 然后,只需重新定义List.length和List.nth。

字符串在ocaml中势在必行,我不认为它在内部使用列表。

As others have pointed out, the statement of the problem is somewhat contradictory. 正如其他人指出的那样,问题的陈述有些矛盾。 All functions are part of some module. 所有功能都是某个模块的一部分。 The most widely used functions are part of the Pervasives module, which is opened automatically for you. 使用最广泛的功能是Pervasives模块的一部分,该模块会自动为您打开。

Assuming the idea is to use only functions from Pervasives, the only available operations I see are the polymorphic comparison operations, the concatenation operator (^) and some I/O operations. 假设仅使用Pervasives中的函数,我看到的唯一可用操作是多态比较操作,串联运算符(^)和某些I / O操作。 You could also include the indexing operator .[] , which is implemented with the help of the compiler. 您还可以包括索引运算符.[] ,它在编译器的帮助下实现。 But you might also exclude it, since it's really just another name for String.get . 但您也可以排除它,因为它实际上只是String.get另一个名称。

This isn't enough to make a useful exercise (as far as I can see). 就我所知,这还不足以进行有用的练习。 You could find the length of the string using I/O operations but I very much doubt that's what you're looking for. 您可以使用I / O操作找到字符串的长度,但我非常怀疑那是您要查找的内容。

let string_length s = (* Not what you really want *)
    let oc = open_out "/tmp/myfile" in
    output_string oc s;
    flush oc;
    let res = out_channel_length oc in
    close_out oc;
    res

It might make more sense to work with a list of characters rather than a string. 使用字符列表而不是字符串可能更有意义。 In OCaml these are completely different types. 在OCaml中,它们是完全不同的类型。 For testing, you could use the following function to change a string to a list of characters. 为了进行测试,您可以使用以下函数将字符串更改为字符列表。 Note that this function doesn't obey your stated limitations: 请注意,此功能不遵守您规定的限制:

let list_of_string s =
    let rec ilist n =
        if n = String.length s then []
        else s.[n] :: ilist (n + 1)
    in
    ilist 0

You can do many things with lists using just pattern matching and the functions of Pervasives. 您可以仅使用模式匹配和Pervasives的功能对列表做很多事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM