Skip to content
Sandny Blog
  • Home
  • Java
  • JavaScript
  • AWS
  • Embedded
  • About
OCaml data structures OCaml

OCaml Data Structures

  • January 25, 2019January 18, 2020
  • by Coder Kai

Ocaml has predefined data structures of tuples, arrays, and lists. There are also mechanisms for defining your own data structures, such as records and variants. In this article, we will discuss about list and tuple.

1. List

List can be define as [ element 1 ; element 2 ; element 3; … ].

Empty List – Empty square brackets [] use to display an empty list.

All elements of a list in OCaml must be the same type. Ex – [1;2;3] ;; [‘a’;’b’];; Otherwise there will be an error as “This expression has type … but an expression was expected of type … “. Values in an array should be separate using semicolons.

A list has two parts as head and tail. List.hd function gives the head of the list and the List.tl gives the tail of the list.

# let numbers=[1;2;3];;
val numbers : int list = [1; 2; 3]
# List.hd numbers;;
- : int = 1
# List.tl numbers;;
- : int list = [2; 3]

So the list can define using cons operator as head :: tail . Therefore [1;2;3] will be equal to the followings;

The operator :: is right-associative. So, 1::2::3 means 1::(2::3).

‘a list – List type can be polymorphic. It means that the type of the elements is anything, but all the elements should be the same type.

List.length function can use to get the length of a list.

# List.length;;
- : 'a list -> int = <fun>
# let numbers=[1;2;3];;
val numbers : int list = [1; 2; 3]
# List.length numbers;;
- : int = 3

Prepend List – You can prepend value to the front of the list as follow;

# let numbers=[1;2;3];;
val numbers : int list = [1; 2; 3]
# (* prepending a element *)
  4::numbers;;
- : int list = [4; 1; 2; 3]

Join Lists – Two lists can be joined using the operator @.

# (* join list *)
  [1;2;3] @ [4;5];;
- : int list = [1; 2; 3; 4; 5]

2. Structures – Tuple 

Unlike lists, tuples can contain elements of different types. Not like other languages, you can’t name the elements. Therefore you have to remember the order in which they appear.

# type tuple = { a : int ; b : int };;
type tuple = { a : int; b : int; }

In structure, all fields must be defined.

Related Articles

  • OCaml Overview
  • Why did Facebook Pick OCaml?
OCaml
Query definition in GraphQL and frequent errors with Apollo Client
Coder Kai
A humble developer
OCaml

Related articles

OCaml
OCaml
Why did Facebook pick OCaml?
Why did Facebook pick OCaml?

Categories

  • android 3
  • Apollo Client 1
  • AWS 8
    • AppSync 5
    • EC2 1
    • EKS 1
    • Route53 1
    • S3 1
  • AWS Amplify 1
  • Chakra UI 1
  • Docker 1
  • Embedded 1
  • EmberJS 1
  • FCM 1
  • Godaddy 1
  • GraphQL 3
  • ios 1
  • Jasper 1
  • Java 10
    • Java 11 1
    • Java 14 1
  • JavaEE 2
  • JavaScript 39
    • Express.js 4
    • Javascript Guide 7
    • Node.js 3
    • react-native 4
    • React.js 17
    • Typescript 1
  • Kubernetes 1
  • machine learning 1
  • Maven 2
  • OCaml 3
  • PostgreSQL 1
  • Python 2
  • react-native 4
  • ReactJS 3
  • sass 1
  • Server 6
  • spark 1
  • Terraform 2
  • Ubuntu 4
  • Uncategorized 1
  • webpack 2

Recent Comments

  • Giselle1803 on How to save pandas Dataframe to a file and read back
  • Alondra579 on How to save pandas Dataframe to a file and read back
  • Aaliyah3552 on How to save pandas Dataframe to a file and read back

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Archives

  • October 2022 3
  • September 2022 7
  • May 2022 1
  • December 2021 1
  • August 2021 1
  • July 2021 6
  • June 2021 3
  • February 2021 1
  • July 2020 1
  • December 2019 5
  • November 2019 6
  • October 2019 3
  • August 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 2
  • December 2018 1
  • September 2018 2
  • August 2018 1
  • June 2018 1
  • February 2018 1
  • November 2017 2
  • October 2017 5
  • September 2017 1
  • June 2017 1
  • May 2017 10
Sandny Blog space
Theme by Colorlib Powered by WordPress