2014-03-07 32 views
0

我需要一个函数,它接受一个字符串和一个字符,它滑动字符串,如果它发现字符返回TRUE其他错误。函数字符串Caml灯

这是我的起点:

let rec check s a = match s with 
    "" -> false 
    | x::xs -> if x = a then true else check xs a ;; 

我不能使用CAML光的库函数(如index_char)

感谢您的帮助!

+0

你的问题是什么? –

+0

您的代码可以完美地处理字符列表。但是一个字符串不是OCaml中的字符列表(尽管有时它会很好)。 –

回答

0

我会离开它是如何工作给你的解释,这是我的解决方案:

[email protected]:~> rlwrap camllight 
>  Caml Light version 0.75  
let findchar c s = 
let len = string_length s in 
let rec f1 i s = 
    if i = len then false 
    else if s.[i]=c then true else f1 (succ i) s in 
    f1 0 s 
;; 
findchar : char -> string -> bool = <fun> 
#let s = "this is the searched string";; 
s : string = "this is the searched string" 
#findchar `a` s;; 
- : bool = true 
#findchar `y` s;; 
- : bool = false 

附加练习:

  • 可能我们在F1定义漏下参数s?
  • 我们如何在f1的主体中调用len的使用/发生?