1

In ML I have an array of chars! I am trying to find an array function in order to give the function the char #"T" and returns me the position of this element in array. I tried the find function and it doesn't work like that!

4

1 回答 1

0

findiArray结构上会做你想要的。对于 type 的数组'a arrayfindi接受 a(int * 'a) -> bool和 a'a array并返回一个(int * 'a) option。因此,如果您想获取一个字符并返回该字符的位置,您只需要找出要传递给的适当参数findi以及解释结果的适当方式。

例如:

- fun findPos char = (Option.map (fn (i,_) => i)) o (Array.findi (fn (_,c) => c = char));
stdIn:2.65 Warning: calling polyEqual
val findPos = fn : ''a -> ''a array -> int option
- findPos #"c" (Array.fromList (explode "abcdef"));
val it = SOME 2 : int option
- findPos #"z" (Array.fromList (explode "abcdef"));
val it = NONE : int option
于 2013-06-13T13:04:02.007 回答