In OCaml, how can I both :
in haskell it is like
f arg@{..} = some code using both arg and its fields
Pattern matching comes up in several places in OCaml: as a powerful control structure combining a multi-armed conditional, unification, data destructuring and variable binding; as a shortcut way of defining functions by case analysis; and as a way of handling exceptions.
What does H :: t mean in OCaml? The pattern h :: t matches any non-empty list, calls the head of the list h (one element, the first one), and the tail of the list t (zero or more elements after the first one).
If you have two variables called x and xs' then x :: xs' creates a new list with x prepended onto the front of xs' .
This is a way to indicate there is no value for an argument. It's necessary to do this in ML because all functions are unary. You can't have a function with zero arguments, so instead you pass an argument containing no information, which is () .
Use as
. E.g.:
let f ((a, b) as original) =
if a > b then
(b, a)
else
original
or:
let g = function
| [] -> []
| (x :: _) as l -> x :: l
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With