Singly-linked list/Element definition: Difference between revisions
Content deleted Content added
m Spelling/grammar/aesthetics |
added ocaml |
||
Line 116: | Line 116: | ||
data IntList s = Nil | Cons Integer (STRef s (IntList s)) |
data IntList s = Nil | Cons Integer (STRef s (IntList s)) |
||
but that would be really awkward to use. |
|||
=={{header|OCaml}}== |
|||
This task is not idiomatic for OCaml. Usually, all data in pure functional programming is immutable, and deconstructed through [[Pattern Matching]]. OCaml already contains a built-in parametrically polymorphic list type that can take any data member type, including numeric values. These lists are then used very frequently. Because of this, lists have additional special syntactic sugar. |
|||
An equivalent declaration for such a list type without the special syntax would look like this: |
|||
type 'a list = Nil | Cons of 'a * 'a list |
|||
A declaration like the one required in the task, with an integer as element type and a mutable link, would be |
|||
type int_list = Nil | Cons of int * int_list ref |
|||
but that would be really awkward to use. |
but that would be really awkward to use. |