cs_lang:ocaml
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cs_lang:ocaml [2011/07/11 14:41] – cedric | cs_lang:ocaml [2012/04/29 11:44] (current) – cedric | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Presentation ====== | ||
+ | ===== Paradigm ==== | ||
+ | * multi-paradigm: | ||
+ | |||
+ | ===== Typing discipline ===== | ||
+ | * static typed; | ||
+ | * strongly typed; | ||
+ | * inferred. | ||
+ | |||
+ | |||
+ | ====== Learning Objective Caml ====== | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ | |||
+ | ====== Simple examples ====== | ||
+ | ===== Increment a number ===== | ||
+ | <code ocaml> | ||
+ | # let inc n = n+1;; | ||
+ | val inc : int -> int = <fun> | ||
+ | |||
+ | # inc 2;; | ||
+ | - : int = 3 | ||
+ | |||
+ | |||
+ | # Alternative: | ||
+ | # let inc = (+) 1;; | ||
+ | val inc : int -> int = <fun> | ||
+ | </ | ||
+ | |||
+ | ==== Equivalent in Python ==== | ||
+ | <code python> | ||
+ | >>> | ||
+ | ... | ||
+ | |||
+ | >>> | ||
+ | 3 | ||
+ | </ | ||
+ | |||
+ | ===== Great common divisor ===== | ||
<code ocaml> | <code ocaml> | ||
- | let rec pgcd a b = match (a mod b) with | + | # let rec gcd a b = match (a mod b) with |
| 0 -> b | | 0 -> b | ||
- | | c -> pgcd b c;; | + | | c -> gcd b c;; |
+ | val gcd : int -> int -> int = < | ||
+ | |||
+ | # gcd 77 42;; | ||
+ | - : int = 7 | ||
</ | </ | ||
+ | ==== Equivalent in Python ==== | ||
+ | <code python> | ||
+ | def gcd(a, b): | ||
+ | while b != 0: | ||
+ | (a, b) = (b, a%b) | ||
+ | return a | ||
+ | </ | ||
+ | ===== Parity ===== | ||
+ | <code ocaml> | ||
+ | let rec parity = function | ||
+ | | [] -> [] | ||
+ | | x::l -> (x mod 2 == 0)::(parity l);; | ||
+ | val func : int list -> bool list = <fun> | ||
+ | |||
+ | parity [1; | ||
+ | - : bool list = [false; true; false; true; false; true; false; true; false] | ||
+ | </ | ||
+ | |||
+ | ==== Equivalent in Python ==== | ||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | [False, True, False, True, False, True, False, True, False] | ||
+ | </ | ||
+ | |||
+ | ===== Ackermann ===== | ||
<code ocaml> | <code ocaml> | ||
let rec ackermann x y = match (x,y) with | let rec ackermann x y = match (x,y) with | ||
Line 12: | Line 82: | ||
</ | </ | ||
+ | ==== Equivalent in Python ==== | ||
+ | <code python> | ||
+ | def ackermann(m, | ||
+ | if m == 0: | ||
+ | return n + 1 | ||
+ | elif n == 0: | ||
+ | return ackermann(m - 1, 1) | ||
+ | else: | ||
+ | return ackermann(m - 1, ackermann(m, | ||
+ | </ | ||
+ | |||
+ | ===== Factorielle ===== | ||
<code ocaml> | <code ocaml> | ||
let rec factorielle = function | let rec factorielle = function |
cs_lang/ocaml.1310388104.txt.gz · Last modified: 2011/07/11 14:41 by cedric