+
, -
, *
, /
/
is the integer division: 7/2 = 3
7 mod 2 = 1
true
and false
&&
, ||
, not
<
, >
, =
, <=
, >=
!
is wrong&&
; "&"
used to be supported but has been removed from OCaml&&
; "and"
has a different meaning5.0 > "hello";;
$\rightarrow$ Error(7.56 <= 8e32) && (6>-3);;
$\rightarrow$ true
false
!true
$\rightarrow$ !
isn't a negation operator!!false
$\rightarrow$ !
isn't a negation operatornot true
not not false
$\rightarrow$ not
is a unary functionnot (not false)
true and false
$\rightarrow$ and
has a completely different meaningtrue && false
true & false
$\rightarrow$ deprecatedfalse or false
$\rightarrow$ deprecatedfalse | false
$\rightarrow$ !
has a completely different meaningtrue || false
false and true
? $\rightarrow$ Syntax errornot false || true
? $\rightarrow$ true
1<2 && 2<>3
? $\rightarrow$ true
(<>
means "not equals")true>false
? $\rightarrow$ true
1=true
? $\rightarrow$ Type error1<2<3
? $\rightarrow$ Type error (comparision between bool
and int
: true<3
)true=1 && not (3=4)
? $\rightarrow$ Type errorfalse<=0 && 3<=4
? $\rightarrow$ Type errortrue<>false && 3<>4
? $\rightarrow$ Type errortrue=1 && not (3=4)
? $\rightarrow$ Type errornot (true>=false) || 3=4
? $\rightarrow$ false
(not true)>=false || 3=4
? $\rightarrow$ true
not true>=false || 3=4
? $\rightarrow$ true
1=2=true
? $\rightarrow$ false
false=1=2
? $\rightarrow$ Type error+.
, -.
, *.
, /.
sqrt
, sin
, cos
, ceil
, floor
.
)float_of_int
: int
$\rightarrow$ float
int_of_float
: float
$\rightarrow$ int
'a'
, '\087'
, etc.Char.chr
: int
$\rightarrow$ char
Char.code
: char
$\rightarrow$ int
Char.code 'A';;
$\rightarrow$ int = 65
String
^
is used for string concatenationString.length
: string
$\rightarrow$ int
int_of_string
: string
$\rightarrow$ int
float_of_string
: string
$\rightarrow$ float
String.get "abcdef" 1;;
$\rightarrow$ char = 'b'
1.5 *. 1e3
$\rightarrow$ 1500.
1.5 *. 1000.
$\rightarrow$ 1500.
1.5 *. 1000
$\rightarrow$ Type error
1.5 *. "1e3"
$\rightarrow$ Type error
1.5 * 1000.
$\rightarrow$ Type error
1.5e3
$\rightarrow$ 1500.
1000. +. 500. /. 2.
$\rightarrow$ 1250.
1.5e3 <= 1500. && 1500 <= 1500 && false <= true
$\rightarrow$ true
1500 < 1500.1
$\rightarrow$ Type error
floor 1500.1 = 1500
$\rightarrow$ Type error
10. /. 3. *. 3.
$\rightarrow$ 10.
sqrt 16. +. 9.
$\rightarrow$ 13.
1. <> 2.5 && 3 <> 4
$\rightarrow$ true
1 <= int_of_float 2.5 && 3. <= floor 3.5
$\rightarrow$ true
not 1. = 2. || not 3 = 4
$\rightarrow$ Type error (there should be brackets as not (1. = 2.)
)1 <= 2.5 && 3 <= 4.5
$\rightarrow$ Type errorif ... then ... else
is an expression, not an instructiontype
is the type of expressions in the then
and else
, which must be the sameif 1=2 then "abc" else "def";;
$\rightarrow$ def
if 1=2 then 3 else 4.5;;
$\rightarrow$ Type errorif (1<>2) then 3 else 4;;
$\rightarrow$ 3
if 1 then 2 else 3;;
$\rightarrow$ Type errorif 1=2 then 34 else "56";;
$\rightarrow$ Type errorif 1<"2" then 3.4 else 5.6;;
$\rightarrow$ Type errorif "Amazone" < "Amour" then 3.4 else 5.6;;
$\rightarrow$ 3.4
if 0 then 1 else 2;;
$\rightarrow$ Type error1 + (if 2=3 then 4. else 5.)
$\rightarrow$ Type errorif (if 1=2 then 3 else 4)<>5 then 6 else 7;;
$\rightarrow$ 6
if 1<>2 then (if 3<>4 then 6 else 7) else 8;;
$\rightarrow$ 6
1 + (if 2=3 then 4 else 5)
$\rightarrow$ 6
if 1<>2 then if 3=4 then 'a' else 'b' else 'c'
$\rightarrow$ b
if 1=2 then (if 3=4 then 'a' else 'b') else (if 'c'<>'d' then 'e' else 'f')
$\rightarrow$ e
if 1=2 then if 3=4 then 5 else 6 else if 'a'<>'b' then 'c' else 'd'
$\rightarrow$ b
In [3]:
if 1<2 then 6+7 else 67/23;;
(if 6=3+3 then 3<4 else 8>7) && 67.8>33.1;;
if (if 1=1 then 2=2 else 4.0>3.2) then 2<3 else 3<2;;
Out[3]:
Out[3]:
Out[3]:
In [4]:
if 6=8 then 1 else 77.5;; (* Error *)
Out[4]:
In [1]:
String.get;;
String.get (string_of_int 65) (int_of_string "0");;
Out[1]:
Out[1]:
(3+5)*5
+
: int -> int-> int
>
: 'a -> 'a -> bool
'a
reads alpha, 'b
reads betalet name = expression
let name = exp1 in exp2
exp2
In [8]:
let x = 4+5 in 2*x;;
x;; (* Error *)
Out[8]:
Out[8]:
In [12]:
let x = 17;;
x;;
let y = x+1 in y/3;;
let x = 4 in
let y = x+1 in
let x = 2*y in x
;;
let x = 4 in
(let x = 17 in x+1) + x
;;
Out[12]:
Out[12]:
Out[12]:
Out[12]:
Out[12]:
In [14]:
let x = 1;;
(* sequential definitions *)
let x = 2 in
let y = x+1 in (* y = 2+1 *)
x*y (* 2*3 *)
;;
(* simultaneous definitions *)
let x = 2 and
y = x+1 in (* y = 1+1 *)
x*y (* 2*2 *)
;;
Out[14]:
Out[14]:
Out[14]:
In [45]:
(* Integer identifiers
*
* Suppose a variable x exists and is an integer.
* Define a variable x_power_8 that uses 3 multiplications
* to calculate x power of 8. The only function you're allowed
* to call is the * operator.
*)
(* The given prelude *)
let a = Random.int 9 + 1;;
let x_power_8 x =
let x1 = x*x in
let x2 = x1*x1 in
x2*x2
;;
x_power_8 a;;
(* Alternate recursive solution using 1 multiplication only *)
let x_power_8_rec n =
let rec aux idx =
if idx = 8
then
1
else
n * aux(idx+1)
in
aux 0
;;
x_power_8_rec a;;
Out[45]:
Out[45]:
Out[45]:
Out[45]:
Out[45]:
In [47]:
(* String identifiers
*
* Suppose that a variable word exists and is a string.
* Define a variable sentence that uses 5 string concatenations
* to create a string containing 9 times word, separated by commas
*)
let word = "foo";;
let x =
word ^ ",";;
let sentence =
let x1 = x ^ x ^ x in
let x2 = x1 ^ x1 in
x2 ^ x1
;;
Out[47]:
Out[47]:
Out[47]:
let f x = exp
let f x = exp1 in exp2
f
to expression e
: f e
In [16]:
let f x = x+1;; (* global definition *)
f 17;;
let g y = 2*y in (* local definition *)
g 42
;;
Out[16]:
Out[16]:
Out[16]:
In [17]:
f f 1;;
Out[17]:
In [18]:
f (f 1);;
Out[18]:
In [22]:
(* with local definition *)
let f x = x+1 in
let g y = f (f y) in
let f x = 2*x in
g 5
;;
(* with global definitions *)
let f x = x+1;;
let g y = f (f y);;
let f x = 2*x;;
g 5;;
Out[22]:
Out[22]:
Out[22]:
Out[22]:
Out[22]:
In [34]:
(* Lexical scoping *)
let a = 1;;
let f x = x+a;;
f 2;;
let a = 73;;
f 2;;
Out[34]:
Out[34]:
Out[34]:
Out[34]:
Out[34]:
In [1]:
(*
* Simple functions: integer
*)
let multiple_of n d =
if n mod d = 0 then true else false
;;
multiple_of 2 10;;
let integer_square_root n =
int_of_float (sqrt (float_of_int n))
;;
integer_square_root 16;;
Out[1]:
Out[1]:
Out[1]:
Out[1]:
In [4]:
(*
* Simple functions: string
*)
let last_character str =
String.get str ((String.length str)-1)
;;
last_character "ANI";;
let string_of_bool truth =
if truth then "true" else "false"
;;
string_of_bool false;;
Out[4]:
Out[4]:
Out[4]:
Out[4]:
f
in a definition of f
refers to the previous value of f
In [36]:
(* Stupid recursion *)
let x = 1;;
let x = x+1;;
x;;
let f x = x+1;;
let f x = f (f x);;
f 1;;
(* Recursive recursion *)
let rec fact n =
if n <= 1 then 1 else n*fact(n-1)
;;
fact 10;;
Out[36]:
Out[36]:
Out[36]:
Out[36]:
Out[36]:
Out[36]:
Out[36]:
Out[36]:
In [40]:
let rec even x =
if x=0 then true else odd(x-1)
and
odd x =
if x=0 then false else even(x-1)
;;
even 17;;
even 24;;
odd 10;;
odd 15;;
Out[40]:
Out[40]:
Out[40]:
Out[40]:
Out[40]:
In [26]:
(*
* Greatest Common Divisor
*)
let rec gcd m n =
if n=0
then
m
else
gcd n (m mod n)
;;
gcd 35 42;;
(*
* Multiple of
*)
let multiple_of n d = n mod d = 0;;
multiple_of 10 2;;
(*
* Multiple upto: int -> int -> bool
* Takes two non-negative integers n and r, and tells whether n admits
* at least one divisor between 2 and r, inclusive
*)
let rec multiple_upto n r =
if r=1
then
false
else
if multiple_of n r
then
true
else
multiple_upto n (r-1)
;;
multiple_upto 7 6;;
(*
* Is Prime?
*)
let is_prime n =
not (multiple_upto n (n-1))
;;
is_prime 37;;
is_prime 16;;
Out[26]:
Out[26]:
Out[26]:
Out[26]:
Out[26]:
Out[26]:
Out[26]:
Out[26]:
Out[26]:
In [27]:
(*
* Consolidated is_prime function
*)
let is_prime n =
let multiple_of n d =
n mod d = 0
in
let rec multiple_upto n r =
if r=1
then
false
else
if multiple_of n r
then
true
else
multiple_upto n (r-1)
in
not (multiple_upto n (n-1))
;;
is_prime 17;;
Out[27]:
Out[27]:
In [35]:
(*
* Alternate version of multiple upto
* The function counts till root of the number for finding multiples
*)
let multiple_upto n r =
let rec aux idx =
if idx > int_of_float (sqrt (float_of_int r))
then
false
else
if n mod idx = 0
then
true
else
aux (idx+1)
in
aux 2
;;
multiple_upto 7 6;;
multiple_upto 8 7;;
Out[35]:
Out[35]:
Out[35]: