Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A list is either the empty list [] or

Two pieces of data (with the constructor ::) with 1 being the head of the list (type 'a) and one being the tail of the list (type 'a list, a recursive definition).

:: is an allowed identifier in oCaml. If that is not allowed, the typical names are Cons for :: and Nil for []



Thanks. I had not understood it could be recursive.

Do you know Cons' meaning ?

Also why does :: act as a separator... ? Is that something you ca do with any Data constructor, I mean can I write type 'a = 'a Cons of 'a | Nil of 'a ?


Cons comes from Lisp: https://en.wikipedia.org/wiki/Cons . In practice a cons cell is a list node represented as a pair of pointers, one pointing to the contained element, the other to the next node (or null, or another element for degenerate lists).


You would write something like List a = Cons a (List a) | Nil

As :: doesn't start with a letter, it's infix by default. But basically a binary constructor. If you want to create the List [1,2,3] in the prefix notation you might be used to: ::(1,::(2,::(3,[])))


Here is the Wikipedia page: https://en.m.wikipedia.org/wiki/Cons




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: