Discussion:
Comparing 'a option in ocaml
(too old to reply)
Jeff M.
2011-04-07 18:56:38 UTC
Permalink
Sorry if this is somewhere on the web, but I've been google'ing my
eyes out looking and haven't found it yet...

Is there a way to compare (==), (>), (<), etc. option values without
writing my own function to do it? For example:

# Some "foo" == Some "foo" ;;
- : bool = false

# Some 10 == Some 10 ;;
- : bool = false

# None == None
- : bool = true

I assume there's a built in function like (val compare : 'a option ->
a' option -> int) that does what I'm looking for, but I haven't been
able to find it.

Thanks!

Jeff M.
Rémi Vanicat
2011-04-07 22:20:55 UTC
Permalink
Post by Jeff M.
Sorry if this is somewhere on the web, but I've been google'ing my
eyes out looking and haven't found it yet...
it's (=)
Post by Jeff M.
Is there a way to compare (==), (>), (<), etc. option values without
it's (=)
Post by Jeff M.
# Some "foo" == Some "foo" ;;
- : bool = false
# Some "foo" = Some "foo";;
- : bool = true
Post by Jeff M.
I assume there's a built in function like (val compare : 'a option ->
a' option -> int) that does what I'm looking for, but I haven't been
able to find it.
The compare function (type 'a -> 'a -> int) have no problem with
optional type
Post by Jeff M.
Thanks!
There is an ocaml beginners mailing list where this kind of question are
very appropriate: http://tech.groups.yahoo.com/group/ocaml_beginners
--
Rémi Vanicat
Jeff M.
2011-04-08 01:30:36 UTC
Permalink
On Apr 7, 4:20 pm, Rémi Vanicat <invalid.newscomp.
Post by Rémi Vanicat
Post by Jeff M.
Sorry if this is somewhere on the web, but I've been google'ing my
eyes out looking and haven't found it yet...
it's (=)
Agh! Thank you very much. I'll head over to the other group you
mention as well (again, thanks), but I'm now curious what the
difference between (=) and (==) is. I assume it's likely similar to
Lisp's EQ vs. EQUAL.
Post by Rémi Vanicat
The compare function (type 'a -> 'a -> int) have no problem with
optional type
Again, thanks. ;-)

Jeff M.
Jon Harrop
2011-04-08 15:59:09 UTC
Permalink
The == operator is reference equality. You almost always want structural
equality (=).

"Jeff M." <***@gmail.com> wrote in message news:ae7711ba-3751-4ca2-ab45-***@x3g2000yqj.googlegroups.com...
On Apr 7, 4:20 pm, Rémi Vanicat <invalid.newscomp.
Post by Rémi Vanicat
Post by Jeff M.
Sorry if this is somewhere on the web, but I've been google'ing my
eyes out looking and haven't found it yet...
it's (=)
Agh! Thank you very much. I'll head over to the other group you
mention as well (again, thanks), but I'm now curious what the
difference between (=) and (==) is. I assume it's likely similar to
Lisp's EQ vs. EQUAL.
Post by Rémi Vanicat
The compare function (type 'a -> 'a -> int) have no problem with
optional type
Again, thanks. ;-)

Jeff M.

Loading...