From what I can tell, = and != is supposed to work on strings in OCaml. I'm seeing strange results though which I would like to understand better.
When I compare two strings with = I get the results I expect:
# "steve" = "steve";;
- : bool = true
# "steve" = "rowe";;
- : bool = false
but when I try != I do not:
# "steve" != "rowe";;
- : bool = true
# "steve" != "steve";; (* unexpected - shouldn't this be false? *)
- : bool = true
Can anyone explain? Is there a better way to do this?
!= is not the negation of =. <> is the negation of = that you should use:
# "steve" <> "rowe" ;;
- : bool = true
# "steve" <> "steve" ;;
- : bool = false
#
!= is the negation of ==, and if you are an OCaml beginner, you should not be using any of these two yet. They can be a little tricky, and they are officially underspecified (the only guarantee is that if two values are == they are =).
Related
This function is part of a string of functions (for a course). It is supposed to take a list of reals [s,a,w,h], and check it against other lists of reals for equality. Those lists of reals are made from converting type racer objects (in the list R::acer) to real lists using racer_stats().
I then want the function to return the Racer that has stats that equal its racer_stats() stats. Unfortunately no matter what I try I cant figure out how to get SML to pattern match [s,a,w,h] as a real list so it will not compare, even when I made a new base case.
Any advice?
fun RFS([s,a,w,h], []) = None
| RFS([s,a,w,h], R::acer) =
if ( [s,a,w,h] = racer_stats(R) )
then R
else RFS([s,a,w,h], acer);
I also tried:
fun RFS( [0.1, 0.2, 0.3] , []) = None
| RFS([s,a,w,h], []) = None
| RFS([s,a,w,h], R::acer) =
if ( [s,a,w,h] = racer_stats(R) )
then R
else RFS([s,a,w,h], acer);
and got a syntax error.
Just in case anyone runs into this later. As molbdnilo pointed out reals are not an equality type. To workaround I built the following comparison operator:
fun compy([], []) = true
| compy([], x) = false
| compy(x, []) = false
| compy(x::xx, y::yy) = ( floor(x*100.0) = floor(y*100.0) ) andalso compy(xx, yy);
The *100.0 was because I wanted to compare to within 2 decimal places. I then swapped compy for =
fun RFS([s,a,w,h], []) = None
| RFS([s,a,w,h], R::acer) = if (compy([s,a,w,h], racer_stats(R)) ) then R
else RFS([s,a,w,h], acer);
Thanks to molbdnilo for pointing out that reals are not an equality type!
Why is this Ocaml statement giving me a syntax error?
let a = 0;; if a = 0 then let b = 0;;
Do if then else statements always have to return a value?
EDIT: Here is the code I am struggling with. I want to apply this function over a list with the map function. The function is supposed to look at each word in the list wordlist and add to the stringmap. If it has already been added to the string map then add 1 to its password.
module StringMap = Map.Make(String)
let wordcount = StringMap.empty
let findword testword =
let wordcount = (if (StringMap.mem testword wordcount)
then (StringMap.add testword ((StringMap.find testword wordcount)+1) wordcount)
else (StringMap.add testword 1 wordcount))
List.map findword wordlist
You can only have an if then without else if the then expression evaluates to unit () Otherwise, the expression will not type check. An if without an else is equivalent to writing if x then y else () which can only type check if y is unit.
Check this out for a reference.
(Terminology note: there are no statements in OCaml because everything is an expression, so the term "if statement" doesn't quite apply. I still understood what you meant, but I thought this was worth noting)
Yes, if is an expression in OCaml, not a statement. The best way to look at it is that there are no statements in OCaml. Everything is an expression. (Admittedly there are expressions that return (), which are similar to statements.)
You can only have if b then e if the type of e is unit (i.e., if it returns ()).
Note also that you can't just say let v = e, except at the top level of a module. At the top level it defines a global name in the module. In other cases you need to say let v = e1 in e2; the let defines a local symbol v for use in the expression e2.
One answer to the let b = problem - it works like this:
let a = 0
let b = if a = 0 then 0 else 1
(* or whatever value you need in the else branch *)
And then the Map problem: the manual says Map is applicative - that means Stringmap.add returns a new map. You must use a ref to store your map - see this ocaml toplevel protocol:
# module StringMap = Map.Make(String);;
# let mymap = ref StringMap.empty ;;
val mymap : '_a StringMap.t ref = {contents = <abstr>}
# mymap := StringMap.add "high" 1 !mymap;;
- : unit = ()
# StringMap.mem "high" !mymap;;
- : bool = true
# StringMap.mem "nono" !mymap;;
- : bool = false
# StringMap.find "high" !mymap;;
- : int = 1
# StringMap.find "nono" !mymap;;
Exception: Not_found.
I am new to SML.
How do I use the AND operator inside IF statements?
Here is my code:
val y = 1;
val x = 2;
if (x = 1 AND y = 2) then print ("YES ") else print("NO ");
My error is:
stdIn:66.9-67.3 Error: unbound variable or constructor: AND
stdIn:66.3-67.9 Error: operator is not a function [literal]
operator: int
in expression:
1
stdIn:66.3-67.9 Error: operator and operand don't agree [literal]
operator domain: bool * bool
operand: bool * int
in expression:
x = (1 ) y = 2
Thank you
There is no AND operator in SML (unless you define one yourself). There is an and keyword, but you can't use it inside if statements (or generally as a part of any expression) because it's not an operator. It's used in combination with fun to define mutually recursive functions.
You're probably looking for the andalso operator, which takes two boolean operands and returns true if and only if both operands are true.
May I disagree with Vishal's comment?
*-true andalso true ;
val it = false : bool
-true andalso false ;
val it = false : bool*
I think (and so does the REPL) that
true andalso true ;
evaluates to true, not false
here is an example which will clear the usage of andalso
-true andalso true ;
val it = false : bool
- true andalso false ;
val it = false : bool
one and one in digital gates is always 1; therefore
in the above case true andalso true "must necessarily evaluate to true , not false; am'i correct ?
also 1 and 0 can't never be 1;
1 or 0 can be 1;
I have a function that return [[]], and I want to test the result as unit test.
But I found that the expression [[]] == [[]] return false.
Here a simple test code:
# [[]] == [[]];;
- : bool = false
Can someone explain me why this expression is evaluated as false?
Thanks.
Use = since you have structural equality for comparing two values:
# [[]] = [[]];;
- : bool = true
Because == is reference equality, it only returns true if you refer to the same memory location:
let a = [[]]
let b = a
# b == a;;
- : bool = true
The == operator in OCaml means "physical equality". However, you have two (physically) different lists. Probably, you want "structural equality", which is tested by =.
It seems to be an equivalency comparison for some types, but not strings.
# 3 != 3;;
- : bool = false
# 3 != 2;;
- : bool = true
This is as expected.
# "odp" = "odp";;
- : bool = true
# "odp" != "odp";;
- : bool = true
# "odp" <> "odp";;
- : bool = false
Why does "odp" != "odp" evaluate to true? What is it actually doing? Shouldn't it generate a type error?
you have experienced the difference between structural and physical equality.
<> is to = (structural equality) as != is to == (physical equality)
"odg" = "odg" (* true *)
"odg" == "odg" (* false *)
is false because each is instantiated in different memory locations, doing:
let v = "odg"
v == v (* true *)
v = v (* true *)
Most of the time you'll want to use = and <>.
edit about when structural and physical equality are equivalent:
You can use the what_is_it function and find out all the types that would be equal both structurally and physically. As mentioned in the comments below, and in the linked article, characters, integers, unit, empty list, and some instances of variant types will have this property.
The opposite for != operator is == operator, not the = one.
# "a" != "a" ;;
- : bool = true
# "a" == "a" ;;
- : bool = false
The == operator is a "physical equality". When you type "a" == "a", you compare two different instances of strings that happen to look alike, so the operator returns false. While having one instance makes it return true:
# let str = "a"
in str == str ;;
- : bool = true
# let str = "a"
in str != str ;;
- : bool = false
A quick explanation about == and != in OCaml in addition to all the correct answers that have already been provided:
1/ == and != expose implementation details that you really don't want to know about. Example:
# let x = Some [] ;;
val x : 'a list option = Some []
# let t = Array.create 1 x ;;
val t : '_a list option array = [|Some []|]
# x == t.(0) ;;
- : bool = true
So far, so good: x and t.(0) are physically equal because t.(0) contains a pointer to the same block that x is pointing to. This is what basic knowledge of the implementation dictates. BUT:
# let x = 1.125 ;;
val x : float = 1.125
# let t = Array.create 1 x ;;
val t : float array = [|1.125|]
# x == t.(0) ;;
- : bool = false
What you are seeing here are the results of an otherwise useful optimization involving floats.
2/ On the other hand, there is a safe way to use ==, and that is as a quick but incomplete way to check for structural equality.
If you are writing an equality function on binary trees
let equal t1 t2 =
match ...
checking t1 and t2 for physical equality is a quick way to detect that they are obviously structurally equal, without even having to recurse and read them. That is:
let equal t1 t2 =
if t1 == t2
then true
else
match ...
And if you keep in mind that in OCaml the “boolean or” operator is “lazy”,
let equal t1 t1 =
(t1 == t2) ||
match ...
They are like two "Tom"s in your class! Because:
In this case, "odp" = "odp"
because they are TWO strings with SAME VALUE!!
So they are not == because they are TWO different strings store in different (Memory) location
They are = because they have the identical string value.
One more step deeper, "odp" is anonymous variable. And two anonymous variable leads to this Two strings.
For your convenience:
# "odp" = "odp";;
- : bool = true
# "odp" != "odp";;
- : bool = true
# "odp" <> "odp";;
- : bool = false
ints are the only type where physical and structural equality are the same, because ints are the only type that is unboxed