# Making a custom user defined List type

**URL:** https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493
**Category:** Learning
**Created:** [October 3, 2019, 11:42pm UTC](https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493 "2019-10-03T23:42:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rene\_sax](https://avatars.discourse-cdn.com/v4/letter/r/0ea827/32.png) [@rene\_sax](https://discuss.ocaml.org/u/rene_sax)
#### Post date: [October 3, 2019, 11:42pm UTC](https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493/1 "2019-10-03T23:42:33Z")

</div>

For the sake of learning, I wanted to define a list. I tried:

```auto
type ‘a list = [] | (::) of ‘a * ‘a list

```

but the top level interpreter got mad at me:

```auto

# type ���a list = [] | (::) of ‘a * ‘a list;;
Alert deprecated: ISO-Latin1 characters in identifiers
Alert deprecated: ISO-Latin1 characters in identifiers
Alert deprecated: ISO-Latin1 characters in identifiers
Error: Illegal character (\128)
# Alert deprecated: ISO-Latin1 characters in identifiers
Alert deprecated: ISO-Latin1 characters in identifiers
Alert deprecated: ISO-Latin1 characters in identifiers
Error: Illegal character (\128) ^CInterrupted.

```

why? How does one define a custom list?

---

<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: [October 4, 2019, 12:05am UTC](https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493/2 "2019-10-04T00:05:13Z")

</div>

```ocaml
(* type def *)
type 'a list =
  | Nil
  | Cons of 'a * ('a list)

(* pattern matching on your type *)
let rec list_length l =
  match l with
  | Nil -> 0
  | Cons (_, s) -> 1 + list_length s

let _ =
  (* defining a value of your type *)
  let l = Cons (1, Cons (2, Cons (3, Nil))) in
  print_int (list_length l)

```

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [October 4, 2019, 3:28am UTC](https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493/3 "2019-10-04T03:28:11Z")

</div>

Your code example has curly quotes. Curly quotes are not valid OCaml syntax. Maybe you typed it in some editor which turns straight quotes into curly quotes? It will work if you use straight quotes. E.g.

```ocaml
module MyList = struct
  type 'a list = [] | (::) of 'a * 'a list
end

```

---

<div class="post-metadata">

### Author: ![rene\_sax](https://avatars.discourse-cdn.com/v4/letter/r/0ea827/32.png) [@rene\_sax](https://discuss.ocaml.org/u/rene_sax)
#### Post date: [October 4, 2019, 3:46am UTC](https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493/4 "2019-10-04T03:46:14Z")

</div>

What does the `'a` mean?

---

<div class="post-metadata">

### Author: ![Chimrod](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chimrod/32/6042_2.png) [@Chimrod](https://discuss.ocaml.org/u/Chimrod)
#### Post date: [October 4, 2019, 7:38am UTC](https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493/5 "2019-10-04T07:38:09Z")

</div>

This is an abstract type, which may be replaced by any one. You can compare `'a list` with `List<A>` in java.

You also can use the abstract type and create a restricted one whih accept only one type of value :

```ocaml
type string_list = string list;;

```

This declaration create another one type, based on the existing one. It only tell the compiler to accept string in this list.

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [October 4, 2019, 2:09pm UTC](https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493/6 "2019-10-04T14:09:42Z")

</div>

`'a` is usually read as ‘alpha’ and is syntactically a _type variable._ A type variable is a ‘stand-in’ for a type that will be filled in later. Type variables are usually also called _generic types._ You can read more here [https://v1.realworldocaml.org/v1/en/html/a-guided-tour.html#inferring-generic-types](https://v1.realworldocaml.org/v1/en/html/a-guided-tour.html#inferring-generic-types)

---

<div class="post-metadata">

### Author: ![rene\_sax](https://avatars.discourse-cdn.com/v4/letter/r/0ea827/32.png) [@rene\_sax](https://discuss.ocaml.org/u/rene_sax)
#### Post date: [October 4, 2019, 2:34pm UTC](https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493/7 "2019-10-04T14:34:06Z")

</div>

but the prime is necessary to define a type variable?

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [October 4, 2019, 2:50pm UTC](https://discuss.ocaml.org/t/making-a-custom-user-defined-list-type/4493/8 "2019-10-04T14:50:48Z")

</div>

Yes. The syntax is covered in the link I gave above 🙂
