Linked List Ocaml?

As aneccodeal told, ocaml already has lists.

As aneccodeal told, ocaml already has lists. However, for your interest, this is how you could build your own list. Obviously, you would never use it on a real application :) If you want to have some trees, the data structure would be very similar.

Exception Empty_list type 'a my_list = Nil | List of 'a * 'a my_list let head = function Nil -> raise Empty_list | List(e,_) -> e;; let tail = function Nil -> Nil | List(_,t) -> t let l = List(1, List(4, List(8, Nil)));; print_endline (string_of_int(head l));; print_endline (string_of_int (head(tail l))).

OCaml has lists built in: List of integers: 1;2;3;4;5 ;; returns: int list = 1; 2; 3; 4 List of Strings: "this";"that";"other";; returns: string list = "this"; "that"; "other" Or you can use the cons operator :: to build lists: 1::2::3::;; returns: int list = 1; 2; 3 To get the head (first item) of a list: List. Hd 1;2;3 returns 1 To get the tail of a list (all items after the first item) List. Tl 1;2;3 returns: int list = 2; 3 Also, you can have a look at how lists are implemented in the OCaml standard library by looking at: installation location for OCaml/lib/ocaml/std-lib/list.ml.

Does that however qualify as a LinkedList, with Nodes and etc? – Faisal Abid Nov 15 '09 at 21:09 @Failsal: yes. Although its really a stack (a data structure built from a linked list), so you can only push and pop nodes from the front of the list. – Juliet Nov 15 '09 at 21:16 1 Just added this at the end of my answer: you can have a look at how lists are implemented in the OCaml standard library by looking at: installation location for OCaml/lib/ocaml/std-lib/list.

Ml – aneccodeal Nov 15 '09 at 21:19 @Juliet, Thats interesting didn't know that! Thanks @annec ill check out the implementation right now thanks! – Faisal Abid Nov 15 '09 at 21:21.

I haven't done SML since college, but I seem to recall head and tail primitives. I see that other people have implemented a true linked list data structure out there though... check out Dustin's OCaml Linkedlist for example.

Hmm let me check that out (the Dustins thing). Thanks – Faisal Abid Nov 15 '09 at 21:11.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions