Hi,
i cant understand the diffirence between these in ocml;
let conf (a,b)=function | (0,true) -> 1| _ -> 5
val conf : 'a * 'b -> int * bool -> int =
this is the most thing that confused me ('a * 'b -> int * bool -> int = ) is there four inputs in this case ?
let conf =function |(0,true)-> 1|_ ->5
val conf : int * bool -> int =
here is input of type tuple (int, bool)
thank you
I think you are missing the main point of anonymous functions - you have to have parameters.
These two are the same:
let conf = function
|(0,true) -> 1
| _ -> 5
let conf v =
match v with
| (0,true) -> 1
| _ -> 5
The keyword function
introduces a new anonymous function with one new parameter.
By the way, in this case you could match using if
directly:
let conf v =
if v = (0,true) then 1
else 5
Hi
Thank you for the that but until now I didn’t understand the the
difference.
@ranamustafa Oh, maybe the problem is about reading the signatures?
Each arrow (->
) in a function signature separates one argument. You can separate them with parenthesis to make it more readable. These are the same thing:
val conf: 'a * 'b -> int * bool -> int
val conf: ('a * 'b) -> (int * bool) -> int
This signature means conf
is a function that expects two arguments (both tuples), and then returns an int
. Notice the first tuple is composed by two anonymous types ('a
and 'b
). OCaml can’t infer them for you, since there are not being used (considering the first implementation of conf
you mentioned).
in :
let conf (a,b)=function | (0,true) -> 1| _ -> 5
you are not using a nor b. This is the same as writing :
let conf (a, b) pattern =
match pattern with
| (0, true) -> 1
| _ -> 5
Maybe you wanted to write :
let conf = function | (0,true) -> 1| _ -> 5
val conf : int * bool -> int = <fun>