x versus _ as x ????
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Sun, 27 Nov 2016 14:06:46 -0600
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJSbcAR+EGo65TXbPzzXXpSaa61S1n3EN6q1kpXupjdbwA@mail.gmail.com> |
Hi everyone,
I created my own little riffle function. It's similar ( but not exactly
the same as ) Wolfram Mathematica's Riffle function. Mathematica's Riffle
output is limited by the length of the smaller list. My riffle output's
length is determined by the length of the larger list. A small difference
really. Anyhow, here is my function:
let riffle list1 list2 = let add_two_lists l1 l2 l3 =
match l1, l2 with
|x, [ ] -> l3 @ x
|[ ], y -> l3 @ y
|h1 :: t1, h2 :: t2 -> h1 :: h2 ::
add_two_lists t1 t2 l3 in
add_two_lists list1 list2 [ ] ;;
It works great! It works exactly as I want it to, BUT HERE'S THE PROBLEM.
The following function DOES NOT EVEN COMPILE!!!! Why????
*let riffle list1 list2 = let add_two_lists l1 l2 l3 = *
* match l1, l2 with *
* |_ as x, [ ] -> l3 @ x*
* |[ ], _ as y -> l3 @ y*
* |h1 :: t1, h2 :: t2 -> h1 :: h2 ::
add_two_lists t1 t2 l3 in *
* add_two_lists list1 list2 [ ] ;;*
Perhaps I do not fully understand how to use the "as" keyword, but I
thought that the two functions should compile and produce the exact same
output. What's the difference between the two forms? I appreciate the
feedback!
Best,
Douglas Lewit.
P.S. Can someone provide a simple example of how to initialize and use a
Map data structure?