Ocaml 为什么这些明显相同的字符串不相等

Ocaml 为什么这些明显相同的字符串不相等,ocaml,equality,string-comparison,Ocaml,Equality,String Comparison,我正在尝试以下代码: open Str let ss = (Str.first_chars "testing" 3);; print_endline ("The first 3 chars of 'testing' are: "^ss);; if (ss == "tes") then print_endline "These are equal to 'tes'" else print_endline "These are NOT equal to 'tes'" 然而,我得到的结果

我正在尝试以下代码:

open Str
let ss = (Str.first_chars "testing" 3);;
print_endline ("The first 3 chars of 'testing' are: "^ss);;
if (ss == "tes") 
  then print_endline "These are equal to 'tes'" 
  else print_endline "These are NOT equal to 'tes'"
然而,我得到的结果是,它们并不相等:

$ ocaml str.cma testing2.ml

The first 3 chars of 'testing' are: tes
These are NOT equal to 'tes'
为什么从“testing”中提取的
Str.first_chars
的前3个字符不等于“tes”

此外,我还必须使用
使此代码工作(我尝试的
中的
的组合无效)。将这三条语句放在一起的最佳方式是什么?

该函数是物理相等运算符。如果要测试两个对象是否具有相同的内容,则应使用具有一个等号的结构相等运算符

将这三个语句组合在一起的最佳方式是什么

OCaml中没有语句。仅表达式,所有返回值。这就像一个数学公式,你有数字、运算符和函数,你把它们组合成更大的公式,例如,
sin(2*pi)
。最接近该语句的是一个具有副作用并返回unit类型值的表达式。但这仍然是一种表达

下面是一个示例,如何构建表达式,它将首先将返回的子字符串绑定到
ss
变量,然后按顺序计算两个表达式:无条件打印和条件打印。总之,这将是一个计算单位值的表达式

open Str

let () = 
  let ss = Str.first_chars "testing" 3 in
  print_endline ("The first 3 chars of 'testing' are: " ^ ss);
  if ss = "tes" 
  then print_endline "These are equal to 'tes'" 
  else print_endline "These are NOT equal to 'tes'"
下面是它的工作原理

$ ocaml str.cma test.ml 
The first 3 chars of 'testing' are: tes
These are equal to 'tes'

因为您使用的是物理相等运算符
==
,而不是结构相等运算符
=
。昨天有人警告过你这件事。您还被告知
之间的区别
中的
。请阅读并试着理解之前给你的答案。