# Wildcards in pattern-matching / recursive function

**URL:** <https://discuss.ocaml.org/t/wildcards-in-pattern-matching-recursive-function/13530>\
**Category:** Learning\
**Tags:** recursion, pattern\
**Created:** [November 29, 2023, 7:51pm UTC](https://discuss.ocaml.org/t/wildcards-in-pattern-matching-recursive-function/13530 "2023-11-29T19:51:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmackiernan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hmackiernan/32/4888_2.png) [@hmackiernan](https://discuss.ocaml.org/u/hmackiernan)\
**Post date:** [November 29, 2023, 7:51pm UTC](https://discuss.ocaml.org/t/wildcards-in-pattern-matching-recursive-function/13530/1 "2023-11-29T19:51:50Z")

</div>

The tutorial I’m following has the following example code for a recursive function that uses pattern matching to return the length of a list

```auto
# let rec length u =
    match u with
    | [] -> 0
    | _ :: v -> 1 + length v;;
val length : 'a list -> int = <fun>

```

Which I read as 'empty list has length zero, things that look like an element cons’d onto a list have length 1 + the length of the sub-list.

In imitation of this, I tried to write a function that returns a string that is the concatenation of all elements in the list.

```auto
let rec glom u = 
match u with 
| [] -> ""
| _ :: v -> _ :: glom v;;
Error: Syntax error: wildcard "_" not expected

```

so it seems like the special character `_` can stand for ‘any element’ in the pattern `_ :: v` to mean “looks like an element cons’d onto a list” but it’s _not_ available as “element that matched”

Is this sort of thing possible in this context? If this were Perl (please don’t throw things) I guess you’d use `$1` to get ‘the thing that matched’

---

<div class="post-metadata">

**Author:** ![ancolie](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ancolie/32/6189_2.png) [@ancolie](https://discuss.ocaml.org/u/ancolie)\
**Post date:** [November 29, 2023, 8:25pm UTC](https://discuss.ocaml.org/t/wildcards-in-pattern-matching-recursive-function/13530/2 "2023-11-29T20:25:11Z")

</div>

First, you can name the head of the list as you wish, for instance `s`:

```ocaml
let rec glom u =
  match u with
  | [] -> ""
  | s :: v -> s :: glom v

```

But then, you’ll get the following error:

```shell-session
$ ocaml tmp.ml
File "./tmp.ml", line 4, characters 14-25:
4 | | s :: v -> s :: glom v
                  ^^^^^^^^^^^
Error: This expression has type 'a list
       but an expression was expected of type string

```

This is because `::` is a constructor for lists.

What you want is the `^` operator that concatenates two strings:

```ocaml
let rec glom u =
  match u with
  | [] -> ""
  | s :: v -> s ^ glom v

```

But note that this is pretty inefficient.

---

<div class="post-metadata">

**Author:** ![hmackiernan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hmackiernan/32/4888_2.png) [@hmackiernan](https://discuss.ocaml.org/u/hmackiernan)\
**Post date:** [November 30, 2023, 7:52pm UTC](https://discuss.ocaml.org/t/wildcards-in-pattern-matching-recursive-function/13530/3 "2023-11-30T19:52:31Z")

</div>

Thanks very much; in retrospect I should have realized I wanted `^` and not `::`
