Jump to content

  •  

Photo

Creating Types and Pattern Matching

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 - 09:21 PM

In OCaml, you are able to create your own Types. In other languages, you can thing of these like enums. You can see how these are created and how they can be used with Pattern Matching.

type month =
  |Jan
  |Feb
  |Mar
  |Apr
  |May
  |Jun
  |Jul
  |Aug
  |Sep
  |Oct
  |Nov
  |Dec
;;

let monthNum month =
  match month with
      |Jan -> 1
      |Feb -> 2
      |Mar -> 3
      |Apr -> 4
      |May -> 5
      |Jun -> 6
      |Jul -> 7
      |Aug -> 8
      |Sep -> 9
      |Oct -> 10
      |Nov -> 11
      |Dec -> 12
;;

monthNum Nov;;

Attached File  Screen Shot 2014-11-02 at 21.20.33.png   81.47KB   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