Home > front end >  What's the difference between a recursive type and a list?
What's the difference between a recursive type and a list?

Time:03-09

I'm pretty new to OCaml (currently taking awful classes at uni) and we recently studied recursive types. We were told that we use that to create lists but there already are lists in OCaml so I don't really understand when I should be using one over the other.

example :

(* list made with a recursive type : *)
type int_list =
    | None
    | Element of int * int_list

(* just a list *)
let int_list2 : int list = [] 

CodePudding user response:

What's the difference between a recursive type and a list?

"Recursive type" describes a whole category of types whereas list is one specific recursive type. In other words: list is a recursive type, but there are recursive types other than list.

there already are lists in OCaml so I don't really understand when I should be using one over the other.

When you should use int_list over the built-in list type? Never. Your teacher showed you this definition as an example of what recursive types look like, not as something you should actually use.

You would define your own recursive types only when defining something that doesn't already exist in the standard library (except for learning exercises of course).

  • Related