ocaml列表反向错误
这是我必须制作回文函数的代码。我之前已经创建了listReverse和explode函数,用于生成回文。有人能帮我芬兰语回文函数吗ocaml列表反向错误,ocaml,Ocaml,这是我必须制作回文函数的代码。我之前已经创建了listReverse和explode函数,用于生成回文。有人能帮我芬兰语回文函数吗 let rec listReverse l = match l with |[] -> [] |head :: tail -> (listReverse tail) @ [head] (* explode : string -> char list * (explode s) is the list of
let rec listReverse l = match l with
|[] -> []
|head :: tail -> (listReverse tail) @ [head]
(* explode : string -> char list
* (explode s) is the list of characters in the string s in the order in
* which they appear
* e.g. (explode "Hello") is ['H';'e';'l';'l';'o']
*)
let explode s =
let rec _exp i =
if i >= String.length s then [] else (s.[i])::(_exp (i+1)) in
_exp 0
let rec palindrome w =
let a = explode w in
let b = listReverse a in
if c :: d
else false
您应该使用标准函数来反转列表。Ocaml是一个自由软件,您应该查看它的实现(文件)尝试用简单的英语(而不是代码)解释您在编写时试图实现的目标
if c :: d
else false
另外,请注意
if foo = bar then true else false
应简化为
foo = bar
您可以将if语句替换为以下内容:
(* tells wheter its a palindrome or not; most is (List.length a)/2*)
let rec same l1 l2 cur most =
match l1, l2 with
| h1::t1, h2::t2 when h1 = h2 ->
if cur < most then same t1 t2 (cur+1) most
else true
| _ -> false in
same a b 0 ((List.length a)/2)
(*表示是否为回文;大多数为(List.length a)/2*)
让rec最接近相同的l1 l2 cur=
将l1、l2与
|当h1=h2->
如果cur在中为假
相同的a b 0((列表长度a)/2)
(这显然是家庭作业。可能不允许在列表中使用函数。)您能告诉我,鉴于字符列表和反向列表,您计划如何测试“回文性”吗?如果您能够解释这一点,那么编写代码可能相当容易。