Jump to content

  •  

Photo

Creating a List

Code Snippet

  • Please log in to reply
No replies to this topic

#1 Wilson18

Wilson18

    Administrator

  • root
  • 112 posts
  • LocationUniversity of Birmingham

Posted 02 November 2014 - 05:43 PM

Here are some useful snippets for when it comes to creating and using lists in OCaml.

(* One List *)
let l  = [0; 1; 2; 3; 4];;
(* Another List *)
let l' = [5; 6; 7; 8; 9];;

(* Appending an item to a list *)
let list1 = 0 :: l;;

Screenshot of above with Syntax Highlighting from emacs

 

Attached File  Screen Shot 2014-11-02 at 17.36.05.png   35.64KB   0 downloads

 

Function to get the first item in the list (head) and to get the rest of the list (tail)

let hd = function
  | [] -> failwith "hd"
  |x :: xs -> x
;;

let tl = function
  |[]        -> failwith "tl"
  | x :: xs  -> xs
;;

hd list1;;
tl list1;;

Attached File  Screen Shot 2014-11-02 at 17.41.12.png   38.23KB   0 downloads

 

 

 

 


List what you read?

Check me out on: YouTube, Twitter, Facebook, Google+






Also tagged with one or more of these keywords: Code Snippet