OCaml Primer (3)

Source: Internet
Author: User
Tags ocaml

If the OPAM is configured in the CentOS environment, OCaml, Core, utop are installed, then you can start learning OCaml.

Follow the footsteps of Real world Ocaml to learn. The HTML version is available online.

Let's take a look.

Common numeric calculations

OCaml care about types, floating-point numbers and integer type operators cannot be mixed.

3 4 ;; int 7  3.1 4.2 floatint3.1      4.2;; float 7.3  

Integer digits are many, for clarity, you can use underscores to separate:

 + ;; int  -

You can bind a value binding to a symbol. Binding at the top level is equivalent to "global variables" in other languages.

Variable names can contain quotation marks (mathematically used) and can start with lowercase letters or underscores .

5int5   utop # Let x'  = 6;; Val x'utop # Let _x = x + x';;

Note that the underscore starts with the variable, Utop does not print its value, but you directly access the value of the variable is possible.
Although the name is "variable", in fact, and other language variables are not the same, OCaml in the variable is "unchanged", OCaml more encourage rebinding, rather than modify the value of the variable.

Many functional languages are this view.

Function

Functions are, of course, at the heart of functional languages.

Functions here are "class citizens", as basic as the int,double in other languages, have types, can be transferred, machinable, can be composite, can be biased and many other tricks.

Utop # let DD x = x *intint = <fun>5;; int  -

Of course, multiple parameters can be

Ten +int int int = <fun>57;; int  $
Tuples

Many high-level language functions can pass in multiple parameters, but can only return a single value at most. If you need to return multiple values is cumbersome.

OCaml provides a tuple type to solve this problem.

Utop # let mydiv x y = x/int int int int. = <fun>  10  3;; int int = (31)  

The tuple type is a comma-separated value, preferably with parentheses outside.
Note the type returned by the system.

OCaml supports "deconstructed assignment", which is convenient for obtaining data from a tuple.

 Utop # Let (A, b) = mydiv 10  3  ;; Val A:  int  = 3   Val B:  int  = 1  
Utop # Let distance (x1,y1) (x2,y2) =  22floatfloat  float float float = <fun>  utop # Distance (1.01.0) (2.02.0);; float 1.41421356237   

The code above shows that "deconstruction" can be used in the parameter position, which is very convenient.

Lists
Utop # let an = ["cat";" Bird "; " Elephant "  string list = ["cat""bird" " Elephant "

Note that the split between elements here is a semicolon, not a comma. Unfortunately, commas are used to divide the tuple elements.

Utop # List.map string.length an;; int list = [348

The spelling of the list is "syntactic sugar", which is actually gradually constructed.

1::2::3:: [];; int list = [123]   

List can be calculated:

Utop # [1;  2; 3] @ [4;  5];; int list = [12345]   

Lists can also be "deconstructed" bindings:

Utop # Let h::t = [1;2;3;4];; Characters4-8: Warning8: ThisPattern-matching isNot exhaustive. here isAn example of a value that isNot matched: []val H:int=1val T:intList = [2;3;4]

List of classic actions, element and the rest of the section

There is a warning, because from a grammatical point of view, if the right side of the list is empty, the left can not be deconstructed, there will be an exception.
Can be avoided by using the match statement, which is equivalent to branching in other languages.

Utop # let F x = match xwith0| (h::t)intint = <fun>  utop # f [1;  2; 3; 4 ];; int 1   utop # f [];; int 0     

OCaml Primer (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.