2

How to compare strings in OCaml? If i have a string a = "abcd" and string z = "Z" z > a is false. String compare z a is negative. I want it to recognize "Z" as bigger than "abcd", is there a specific function for this?

objmagic
  • 1,028
  • 1
  • 9
  • 18
power_output
  • 411
  • 10
  • 26
  • 1
    Is it possible for you to use `String.uppercase_ascii`? (works since OCaml v. 4.03.0). `String.uppercase_ascii a < z` returns `true`. The problem is in the fact that in ASCII 'a' > 'Z'. – Anton Trunov May 30 '16 at 12:47
  • You should provide some more context preferably in the form of OCaml code. – Anton Trunov May 30 '16 at 12:51
  • I searched for "case insensitive string comparison ocaml" and found a [reference](http://stackoverflow.com/questions/2030863/in-functional-programming-what-is-a-functor/2031086?s=1|0.2538#2031086) to a possible way to solve this problem. – Stefan Schmiedl May 30 '16 at 15:49

1 Answers1

3

The generic comparison function provided by Ocaml is guaranteed to work for any type and to be a total order but that's all. In particular there is no guarantee that it corresponds to a meaningful order. So if you don't like it just use something else, for instance using < instead of > will give you the result you want for "abcd" and "Z".

Thomash
  • 6,339
  • 1
  • 30
  • 50