简体   繁体   中英

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

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 = [] 

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM