` operator in OCaml - ocaml

What does the ` operator do in OCaml?
let int_of_meth = function
| `GET -> 0
| `POST -> 1
| `PUT -> 2
| `DELETE -> 3
| `HEAD -> 4
| `PATCH -> 5
| `OPTIONS -> 6
| _ -> failwith "non standard http verbs not supported"
I can't find it in the OCaml manual.

This ` is not really an operator. It works at the lexical level (like quotes for strings) and makes the following symbol into a "polymorphic variant". See the link given by #Edgar Aroutiounian:
http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual006.html#toc36
Update
Actually, ` is scanned as a separate symbol as noted by #gsg. So a polymorphic variant like ` Abc is a syntactic construct. I would still claim it's not an operator in the usual sense.
(Edit: changed to Abc. I never knew they were supposed to be capitalized. For example, the lablgl interface seems to use lower case consistently.)

Related

This expression has type processor_return_type but an expression was expected of type unit

I now have a function called processor, inside the processor function, a list will be matched to different patterns. In some patterns I wish it to return a tuple while the rest calls processor again.
Suppose I now have a custom type to wrap two types of processor:
type processor_return_type =
| REC of unit
| INFO of (string list * bool)
My processor basically looks like this:
let rec processor cmds stack env =
match (cmds, stack) with
| (ADD::rest_cmds, first_list::rest_stack) -> ... processor a b c
...
| (FUN::...) -> ... let (sl, b) = processor a b c in processor d e f
| (RETURN::...) -> (string list, a bool)
| _ -> REC()
...
in
Then I invoke this function with (you can assume I give correct arguments):
processor cmd_list [[]] [[]];;
The error emerges:
664 | processor cmd_list [[]] [[]];;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This expression has type processor_return_type
but an expression was expected of type unit
How could I fix this issue?
Thanks for including the error message and the indicated erroneous part of the code.
The expression that the compiler is complaining about is the entire call to processor. In other words, the reason the compiler is expecting something of type unit is because of the context of the call, which you unfortunately don't show.
Generally your code should expect to get a result back from the call to processor and should handle the result appropriately. If there are other branches of your code (the else part of an if, say), they have to be of the same type also.
Here's an example of an erroneous call context:
if some_test () then
Printf.printf "did not want to call processor\n"
else
proceessor ...
If you show more of the context of the call, people can give you a more helpful answer maybe.

Warning appear after I solve a recursion question in Haskell

I'm trying to implementing a code question below, but I got this type of warning. I don't know what happened since I could output the answer correctly. Below is my code and the warning:
continuous :: [Integer] -> Bool
continuous list = case list of
[] -> True
[x,y]
| abs (x-y) <= 1 -> True
| otherwise -> False
x:y:xs
| abs(x-y) <= 1 -> continuous (y:xs)
| otherwise -> False
Lists.hs:43:19: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In a case alternative: Patterns not matched: [_]
|
43 | continuous list = case list of
| ^^^^^^^^^^^^^...
The warning notes, that your pattern matching is not exhaustive, since you are missing the case for a list with a single element, [x] for example.
Additionally, if you add a case for a single element, the case [x,y] is not needed anymore, since it is also handled by the case x:y:xs, because xs can also represent an empty list

why the use of _ prevent having warnings

I have the following code (it's a test so it does nothing interesting)
let test k =
let rec aux = function
|0 -> 0
|z when z = 2 -> raise Exit
|_ -> aux (k-1)
in try let _ = aux k in true
with Exit -> false
At the end there is the use of the syntax : let _, to me it's just a syntax when you don't have an idea of a name you can use to define your function.
Yet if I do the following :
let test k =
let rec aux = function
|0 -> 0
|z when z = 2 -> raise Exit
|_ -> aux (k-1)
in try let b = aux k in true
with Exit -> false
I get a warning like : "variable b is unused", I don't understand why there is a difference between let _ and let b ?
For example I know that when dealing with unit type it's common to use the syntax : let (). Yet I don't have any warning when doing :
let b = print_int 2
even if I am not using :
let () = print_int 2
So what is particular with let _ ?
Thank you !
This is a convention, recognized by the compiler, to indicate that you're not going to use the result of a computation, e.g.,
let a = 5 + 6 in
()
will (or will not, depending on your warning settings) trigger the unused variable warning, since you clearly bound the result to a variable a, but not using it in the rest of your computation. In imperative languages it is quite common, to compute expressions for their side effects and ignore produced values if any. Since OCaml is a functional language, in which values are used to produce values, it usually an indicator of an error, when you forgot to use a bound variable.
Therefore, to explicitly tell the compiler that you're ignoring the value, you may start your variable with the underscore, e.g.,
let _unusued = 5 + 6 in
()
You can just use a wild pattern _ (which also starts with the underscore).
You have a warning with your second code because you define the variable b containing a value and you do not use it after.
The best use if you do not want to use the result of any expression is to discard its result using the 'let _ =' construct (it tells you want the expression to be evaluated, for potential side effects, but do not care to keep its result).
For the second part of your question, I think there are different rules related to the top loop, so the behaviours may not be comparable. In the first part, you define b inside a function and in the second part, you define b inside the top loop. In the top loop, you may define variables you will not use without getting a warning.

In Robot Framework, what is the difference between a List Variable and a Scalar Variable containing a list?

In Robot Framework, we can assign a list to a Scalar Variable or to a List Variable, as shown below:
| #{list} = | Create List | a | b | c |
| ${scalar} = | Create List | a | b | c |
What is the difference between a List Variable and a Scalar Variable containing a list?
In case of the assignment shown in your question, there is no difference. If you log each of those you'll get the exact same output.
Note: this functionality was introduced in version 2.8 (see Using scalar variables as lists in Robot Framework User's Guide).
The difference comes when you use the values. When you use the # symbol to reference a list, each of the elements in the list becomes a cell. In the following example, the following three lines give identical results:
| | A keyword that expects three arguments | a | b | c
| | A keyword that expects three arguments | #{list}
| | A keyword that expects three arguments | #{scalar}

OCaml Option get

I'm new to OCaml, I'm trying to understand how you're supposed to get the value from an 'a option. According to the doc at http://ocaml-lib.sourceforge.net/doc/Option.html, there is a get function of type 'a option -> 'a that does what I want. but when I type:
# let z = Some 3;;
val z : int option = Some 3
# get z;;
Error: Unbound value get
# Option.get z;;
Error: Unbound module Option
Why isnt this working?
The traditional way to obtain the value inside any kind of constructor in OCaml is with pattern-matching. Pattern-matching is the part of OCaml that may be most different from what you have already seen in other languages, so I would recommend that you do not just write programs the way you are used to (for instance circumventing the problem with ocaml-lib) but instead try it and see if you like it.
let contents =
match z with
Some c -> c;;
Variable contents is assigned 3, but you get a warning:
Warning 8: this pattern-matching is not exhaustive. Here is an example
of a value that is not matched: None
In the general case, you won't know that the expression you want to look inside is necessarily a Some c. The reason an option type was chosen is usually that sometimes that value can be None. Here the compiler is reminding you that you are not handling one of the possible cases.
You can pattern-match “in depth” and the compiler will still check for exhaustivity. Consider this function that takes an (int option) option:
let f x =
match x with
Some (Some c) -> c
| None -> 0
;;
Here you forgot the case Some (None) and the compiler tells you so:
Warning 8: this pattern-matching is not exhaustive. Here is an example
of a value that is not matched: Some None
The usual way to do this is with pattern matching.
# let x = Some 4;;
val x : int option = Some 4
# match x with
| None -> Printf.printf "saw nothing at all\n"
| Some v -> Printf.printf "saw %d\n" v;;
saw 4
- : unit = ()
You can write your own get function (though you have to decide
what you want to do when the value is None).
You should listen to the above posters advice regarding type safety but also be aware that unsafe function such as Option.get (which is available in batteries btw) are usually suffixed with exn. If you're curious this is how Option.get or Option.get_exn could be implemented then:
let get_exn = function
| Some x -> x
| None -> raise (Invalid_argument "Option.get")