From 8e11620f277bb9c2f6e657de4b2acecb6308c57c Mon Sep 17 00:00:00 2001
From: nandorpersanyi To get the element number Operations on arrays
i of an array, you have two
-ways to do this (don't forget, the first element has number 0):0):
Array.get [| 42; 51; 32 |] 2
or
diff --git a/ocaml-lessons/lesson1/step6/step.html b/ocaml-lessons/lesson1/step6/step.html index 592b808..461e619 100644 --- a/ocaml-lessons/lesson1/step6/step.html +++ b/ocaml-lessons/lesson1/step6/step.html @@ -1,15 +1,15 @@A tuple consists of values (zero or more), enclosed in parentheses -and separated by commas. Note that you can have differents types for -each element, remember
+and separated by commas. Note that you can have different types for +each element:(42, "John", true)
When you have a pair (a tuple with two elements), you can use some -predefined functions like get the first element:
+When you have a pair (a tuple with two elements), you can use +predefined functions like getting the first element:
fst (42, "John")
Or the second element:
+or the second element:
snd (42, "John")
As in other languages, you can associate a name to a value. To do
-that, we use the let syntax which associates the result
-of some computation with a name:
Similarly to other languages, you can associate user defined variable names with values. To do
+that, we use the let syntax which associates the result of value declaration, or some computation with a user defined variable name:
let a = 42
let x = 6 * 7
We can now check the value associated with x in the
+
We can now check the value associated with a, or x in the
toplevel:
a
x
And we can use the name where we would like the value:
-let y = x + 1
And we can just use the variable name where we would like to access the value:
+let y = a + x
In OCaml, you cannot change the value associated with a name after +
In OCaml, you cannot change the value associated with a variable after its definition. Trying to do so will trigger a compilation error:
x <- x + 1
You will better understand the error message later.
If you really want to modify the value associated with a name, you
-must use a trick. OCaml provides a function ref that
-creates a special value that can be modified later (a reference):
If you really want to modify the value associated with a variable, you
+must use a "trick". OCaml provides the ref syntax, that
+creates a special value, that can be modified later (a reference):
let x = ref 42
x := 100 / 4
You can also access the value contained in the reference using the +
You can access the value contained in the reference using the
operator !:
let y = !x + 1
A more powerful method to print values
-is Printf.printf, whose behavior is similar
+is Printf.printf, which is similar
to printf in C:
Printf.printf "x = %d. Bye %s\n" !x "John"
Here, for each value between 1 and 10, we have added it in this -order at the head of the reference.
+Here, we have added each value between 1 and 10 at the head of the reference.
Of course, if we want them in the correct order, we need to reverse the list:
diff --git a/ocaml-lessons/lesson2/step6/step.html b/ocaml-lessons/lesson2/step6/step.html index ec8fc38..c3c90cb 100644 --- a/ocaml-lessons/lesson2/step6/step.html +++ b/ocaml-lessons/lesson2/step6/step.html @@ -1,7 +1,7 @@ -Now that we know how to test conditions, we can use them to choose -between computations. Let's define two values :
+between computations. Let's define two values:let a = 1 and b = 2
In OCaml defining a function with one argument, will look like this:
let incr n = n + 1
In C or Java, a function plus, will look like:
int plus (int x, int y) { return x + y; }
@@ -8,10 +8,10 @@plus (1, 2);
In OCaml, the same function plus will be define like follow:
In OCaml, the same function plus will be defined like this:
let plus x y = x + y
To call this function, nothing is simpler:
plus 1 2
Note that there is no need to bracket or comma between function's parameters.
+Note that there is no need for brackets, or a comma between the function's parameters.
diff --git a/ocaml-lessons/lesson3/step4/step.html b/ocaml-lessons/lesson3/step4/step.html index f69649d..8caa07e 100644 --- a/ocaml-lessons/lesson3/step4/step.html +++ b/ocaml-lessons/lesson3/step4/step.html @@ -1,6 +1,6 @@It is possible to apply a number of parameters less than what is +
It is possible to apply less parameters than what is required by a function. The result would be a partial application of a function.
@@ -21,8 +21,8 @@incr 42
Similarly, we can define a function which double each integer -passed as argument of the function:
+Similarly, we can define a function which will double each integer +passed as an argument to it:
let mul x y = x * y
let double = mul 2
double 8
diff --git a/ocaml-lessons/lesson3/step5/step.html b/ocaml-lessons/lesson3/step5/step.html index 1e2fd8b..0c3a7f6 100644 --- a/ocaml-lessons/lesson3/step5/step.html +++ b/ocaml-lessons/lesson3/step5/step.html @@ -5,7 +5,7 @@
(fun x -> x + 1) 42
We can bound an anonymous function to an identifier. That's way, we -have severals ways to define functions:
+We can also bound anonymous functions to identifiers (variable names). Now we +have several ways to define functions:
let incr = fun x -> x + 1
incr 42
let capitalize = function
diff --git a/ocaml-lessons/lesson4/step4/step.html b/ocaml-lessons/lesson4/step4/step.html
index 93a14c5..1ba0189 100644
--- a/ocaml-lessons/lesson4/step4/step.html
+++ b/ocaml-lessons/lesson4/step4/step.html
@@ -2,12 +2,12 @@ Pattern-matching on lists
However, the real power of pattern-matching appears when we start
- using more structured values, when we start needing giving a name to
- matched patterns. For instance, a list is either the empty
+ using more structured values, and when the need appears for assigning variable names to
+ matched patterns. For instance, a list is either an empty
list [] or a head and a tail, denoted by the
pattern h::t where h and t are fresh
variables bound to the matched patterns:
-
+
let head = function
| [] -> failwith "empty list"
| h::t -> h
diff --git a/ocaml-lessons/lesson4/step5/step.html b/ocaml-lessons/lesson4/step5/step.html
index e54ef37..cac5123 100644
--- a/ocaml-lessons/lesson4/step5/step.html
+++ b/ocaml-lessons/lesson4/step5/step.html
@@ -1,13 +1,13 @@
Pattern-matching on arrays
-You can also pattern match on arrays:
+
You can also use pattern-matching on arrays:
let has_size_two = function
| [| _; _ |] -> true
| _ -> false
-And you can mix all the kind of values:
+And you can mix all kinds of values:
let f = function
| [] -> failwith "empty list"
diff --git a/ocaml-lessons/lesson5/step1/step.html b/ocaml-lessons/lesson5/step1/step.html
index b406b2a..64771cd 100644
--- a/ocaml-lessons/lesson5/step1/step.html
+++ b/ocaml-lessons/lesson5/step1/step.html
@@ -5,9 +5,9 @@ Sequence of expressions
any deeper.
-To begin with, you should know that a proper command must normally ends with
-';;' to be processed by the top-level. This tutorial automatically adds
-the double semicolon as soon as you hit enter but the normal top-level won't.
+
To begin with, you should know that a proper command must normally end with
+';;' to be processed by the top-level interpreter. This tutorial automatically adds
+the double semicolon as soon as you hit enter but the normal top-level interpreter won't.
The double semicolon is only required when interacting with the top level
interpreter and as such is not part of OCaml syntax.
diff --git a/ocaml-lessons/lesson5/step3/step.html b/ocaml-lessons/lesson5/step3/step.html
index c966030..fc4b8ba 100644
--- a/ocaml-lessons/lesson5/step3/step.html
+++ b/ocaml-lessons/lesson5/step3/step.html
@@ -11,10 +11,10 @@ The let keyword
The let keyword is also used to form an expression in which a name
-is given to some value temporarily, for the evaluation of a subexpression only:
+is associated with some value temporarily, for the evaluation of a subexpression only:
let x = 41 in x + 1
- The value of x is 41 during
+The value of x is 41 during
the evaluation of x + 1 only; the global binding of x to
"I am now a string!" is preserved.
diff --git a/ocaml-lessons/lesson5/step5/step.html b/ocaml-lessons/lesson5/step5/step.html
index 1fab8a1..9891abe 100644
--- a/ocaml-lessons/lesson5/step5/step.html
+++ b/ocaml-lessons/lesson5/step5/step.html
@@ -4,7 +4,7 @@ Parentheses
OCaml syntax is surprisingly easy: you can use pervasively either parentheses
or begin/end keywords.
-Example grouping expressions in an if form:
+Example of grouping expressions in an if form:
if 1+2 = 3 then (
print_string "did you knew that?\n" ;
print_string "amazing!\n"
@@ -16,8 +16,8 @@ Parentheses
begin 1 + 2 end * 3
-Also, as function application takes precedence over infix operators you will
-frequently uses parentheses to make explicit the expected evaluation order, as
+
Also, as function application takes precedence over infix operators, you will
+frequently uses parentheses to make the expected evaluation order more explicit, as
in: square (1 + 1) since square 1+1 would yield
2.